gcinode.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. * Since NILFS2 keeps up multiple checkpoints/snapshots accross GC, it
  32. * has to treat blocks that belong to a same file but have different
  33. * checkpoint numbers. To avoid interference among generations, dummy
  34. * inodes are managed separatly from actual inodes, and their lookup
  35. * function (nilfs_gc_iget) is designed to be specified with a
  36. * checkpoint number argument as well as an inode number.
  37. *
  38. * Buffers and pages held by the dummy inodes will be released each
  39. * time after they are copied to a new log. Dirty blocks made on the
  40. * current generation and the blocks to be moved by GC never overlap
  41. * because the dirty blocks make a new generation; they rather must be
  42. * written individually.
  43. */
  44. #include <linux/buffer_head.h>
  45. #include <linux/mpage.h>
  46. #include <linux/hash.h>
  47. #include <linux/swap.h>
  48. #include "nilfs.h"
  49. #include "page.h"
  50. #include "mdt.h"
  51. #include "dat.h"
  52. #include "ifile.h"
  53. static struct address_space_operations def_gcinode_aops = {};
  54. /* XXX need def_gcinode_iops/fops? */
  55. /*
  56. * nilfs_gccache_submit_read_data() - add data buffer and submit read request
  57. * @inode - gc inode
  58. * @blkoff - dummy offset treated as the key for the page cache
  59. * @pbn - physical block number of the block
  60. * @vbn - virtual block number of the block, 0 for non-virtual block
  61. * @out_bh - indirect pointer to a buffer_head struct to receive the results
  62. *
  63. * Description: nilfs_gccache_submit_read_data() registers the data buffer
  64. * specified by @pbn to the GC pagecache with the key @blkoff.
  65. * This function sets @vbn (@pbn if @vbn is zero) in b_blocknr of the buffer.
  66. *
  67. * Return Value: On success, 0 is returned. On Error, one of the following
  68. * negative error code is returned.
  69. *
  70. * %-EIO - I/O error.
  71. *
  72. * %-ENOMEM - Insufficient amount of memory available.
  73. *
  74. * %-ENOENT - The block specified with @pbn does not exist.
  75. */
  76. int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
  77. sector_t pbn, __u64 vbn,
  78. struct buffer_head **out_bh)
  79. {
  80. struct buffer_head *bh;
  81. int err;
  82. bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
  83. if (unlikely(!bh))
  84. return -ENOMEM;
  85. if (buffer_uptodate(bh))
  86. goto out;
  87. if (pbn == 0) {
  88. struct inode *dat_inode = NILFS_I_NILFS(inode)->ns_dat;
  89. /* use original dat, not gc dat. */
  90. err = nilfs_dat_translate(dat_inode, vbn, &pbn);
  91. if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
  92. brelse(bh);
  93. goto failed;
  94. }
  95. }
  96. lock_buffer(bh);
  97. if (buffer_uptodate(bh)) {
  98. unlock_buffer(bh);
  99. goto out;
  100. }
  101. if (!buffer_mapped(bh)) {
  102. bh->b_bdev = NILFS_I_NILFS(inode)->ns_bdev;
  103. set_buffer_mapped(bh);
  104. }
  105. bh->b_blocknr = pbn;
  106. bh->b_end_io = end_buffer_read_sync;
  107. get_bh(bh);
  108. submit_bh(READ, bh);
  109. if (vbn)
  110. bh->b_blocknr = vbn;
  111. out:
  112. err = 0;
  113. *out_bh = bh;
  114. failed:
  115. unlock_page(bh->b_page);
  116. page_cache_release(bh->b_page);
  117. return err;
  118. }
  119. /*
  120. * nilfs_gccache_submit_read_node() - add node buffer and submit read request
  121. * @inode - gc inode
  122. * @pbn - physical block number for the block
  123. * @vbn - virtual block number for the block
  124. * @out_bh - indirect pointer to a buffer_head struct to receive the results
  125. *
  126. * Description: nilfs_gccache_submit_read_node() registers the node buffer
  127. * specified by @vbn to the GC pagecache. @pbn can be supplied by the
  128. * caller to avoid translation of the disk block address.
  129. *
  130. * Return Value: On success, 0 is returned. On Error, one of the following
  131. * negative error code is returned.
  132. *
  133. * %-EIO - I/O error.
  134. *
  135. * %-ENOMEM - Insufficient amount of memory available.
  136. */
  137. int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
  138. __u64 vbn, struct buffer_head **out_bh)
  139. {
  140. int ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache,
  141. vbn ? : pbn, pbn, out_bh, 0);
  142. if (ret == -EEXIST) /* internal code (cache hit) */
  143. ret = 0;
  144. return ret;
  145. }
  146. int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
  147. {
  148. wait_on_buffer(bh);
  149. if (!buffer_uptodate(bh))
  150. return -EIO;
  151. if (buffer_dirty(bh))
  152. return -EEXIST;
  153. if (buffer_nilfs_node(bh))
  154. nilfs_btnode_mark_dirty(bh);
  155. else
  156. nilfs_mdt_mark_buffer_dirty(bh);
  157. return 0;
  158. }
  159. /*
  160. * nilfs_init_gccache() - allocate and initialize gc_inode hash table
  161. * @nilfs - the_nilfs
  162. *
  163. * Return Value: On success, 0.
  164. * On error, a negative error code is returned.
  165. */
  166. int nilfs_init_gccache(struct the_nilfs *nilfs)
  167. {
  168. int loop;
  169. BUG_ON(nilfs->ns_gc_inodes_h);
  170. INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
  171. nilfs->ns_gc_inodes_h =
  172. kmalloc(sizeof(struct hlist_head) * NILFS_GCINODE_HASH_SIZE,
  173. GFP_NOFS);
  174. if (nilfs->ns_gc_inodes_h == NULL)
  175. return -ENOMEM;
  176. for (loop = 0; loop < NILFS_GCINODE_HASH_SIZE; loop++)
  177. INIT_HLIST_HEAD(&nilfs->ns_gc_inodes_h[loop]);
  178. return 0;
  179. }
  180. /*
  181. * nilfs_destroy_gccache() - free gc_inode hash table
  182. * @nilfs - the nilfs
  183. */
  184. void nilfs_destroy_gccache(struct the_nilfs *nilfs)
  185. {
  186. if (nilfs->ns_gc_inodes_h) {
  187. nilfs_remove_all_gcinode(nilfs);
  188. kfree(nilfs->ns_gc_inodes_h);
  189. nilfs->ns_gc_inodes_h = NULL;
  190. }
  191. }
  192. static struct inode *alloc_gcinode(struct the_nilfs *nilfs, ino_t ino,
  193. __u64 cno)
  194. {
  195. struct inode *inode = nilfs_mdt_new_common(nilfs, NULL, ino, GFP_NOFS);
  196. struct nilfs_inode_info *ii;
  197. if (!inode)
  198. return NULL;
  199. inode->i_op = NULL;
  200. inode->i_fop = NULL;
  201. inode->i_mapping->a_ops = &def_gcinode_aops;
  202. ii = NILFS_I(inode);
  203. ii->i_cno = cno;
  204. ii->i_flags = 0;
  205. ii->i_state = 1 << NILFS_I_GCINODE;
  206. ii->i_bh = NULL;
  207. nilfs_bmap_init_gc(ii->i_bmap);
  208. return inode;
  209. }
  210. static unsigned long ihash(ino_t ino, __u64 cno)
  211. {
  212. return hash_long((unsigned long)((ino << 2) + cno),
  213. NILFS_GCINODE_HASH_BITS);
  214. }
  215. /*
  216. * nilfs_gc_iget() - find or create gc inode with specified (ino,cno)
  217. */
  218. struct inode *nilfs_gc_iget(struct the_nilfs *nilfs, ino_t ino, __u64 cno)
  219. {
  220. struct hlist_head *head = nilfs->ns_gc_inodes_h + ihash(ino, cno);
  221. struct hlist_node *node;
  222. struct inode *inode;
  223. hlist_for_each_entry(inode, node, head, i_hash) {
  224. if (inode->i_ino == ino && NILFS_I(inode)->i_cno == cno)
  225. return inode;
  226. }
  227. inode = alloc_gcinode(nilfs, ino, cno);
  228. if (likely(inode)) {
  229. hlist_add_head(&inode->i_hash, head);
  230. list_add(&NILFS_I(inode)->i_dirty, &nilfs->ns_gc_inodes);
  231. }
  232. return inode;
  233. }
  234. /*
  235. * nilfs_clear_gcinode() - clear and free a gc inode
  236. */
  237. void nilfs_clear_gcinode(struct inode *inode)
  238. {
  239. nilfs_mdt_clear(inode);
  240. nilfs_mdt_destroy(inode);
  241. }
  242. /*
  243. * nilfs_remove_all_gcinode() - remove all inodes from the_nilfs
  244. */
  245. void nilfs_remove_all_gcinode(struct the_nilfs *nilfs)
  246. {
  247. struct hlist_head *head = nilfs->ns_gc_inodes_h;
  248. struct hlist_node *node, *n;
  249. struct inode *inode;
  250. int loop;
  251. for (loop = 0; loop < NILFS_GCINODE_HASH_SIZE; loop++, head++) {
  252. hlist_for_each_entry_safe(inode, node, n, head, i_hash) {
  253. hlist_del_init(&inode->i_hash);
  254. list_del_init(&NILFS_I(inode)->i_dirty);
  255. nilfs_clear_gcinode(inode); /* might sleep */
  256. }
  257. }
  258. }