gcinode.c 8.0 KB

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