gcinode.c 8.0 KB

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