gcinode.c 8.1 KB

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