2 * gcinode.c - dummy inodes to buffer blocks for garbage collection
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
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
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>.
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.
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.
38 #include <linux/buffer_head.h>
39 #include <linux/mpage.h>
40 #include <linux/hash.h>
41 #include <linux/slab.h>
42 #include <linux/swap.h>
52 * nilfs_gccache_submit_read_data() - add data buffer and submit read request
54 * @blkoff - dummy offset treated as the key for the page cache
55 * @pbn - physical block number of the block
56 * @vbn - virtual block number of the block, 0 for non-virtual block
57 * @out_bh - indirect pointer to a buffer_head struct to receive the results
59 * Description: nilfs_gccache_submit_read_data() registers the data buffer
60 * specified by @pbn to the GC pagecache with the key @blkoff.
61 * This function sets @vbn (@pbn if @vbn is zero) in b_blocknr of the buffer.
63 * Return Value: On success, 0 is returned. On Error, one of the following
64 * negative error code is returned.
68 * %-ENOMEM - Insufficient amount of memory available.
70 * %-ENOENT - The block specified with @pbn does not exist.
72 int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
73 sector_t pbn, __u64 vbn,
74 struct buffer_head **out_bh)
76 struct buffer_head *bh;
79 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
83 if (buffer_uptodate(bh))
87 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
89 err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
90 if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
97 if (buffer_uptodate(bh)) {
102 if (!buffer_mapped(bh)) {
103 bh->b_bdev = inode->i_sb->s_bdev;
104 set_buffer_mapped(bh);
107 bh->b_end_io = end_buffer_read_sync;
117 unlock_page(bh->b_page);
118 page_cache_release(bh->b_page);
123 * nilfs_gccache_submit_read_node() - add node buffer and submit read request
125 * @pbn - physical block number for the block
126 * @vbn - virtual block number for the block
127 * @out_bh - indirect pointer to a buffer_head struct to receive the results
129 * Description: nilfs_gccache_submit_read_node() registers the node buffer
130 * specified by @vbn to the GC pagecache. @pbn can be supplied by the
131 * caller to avoid translation of the disk block address.
133 * Return Value: On success, 0 is returned. On Error, one of the following
134 * negative error code is returned.
138 * %-ENOMEM - Insufficient amount of memory available.
140 int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
141 __u64 vbn, struct buffer_head **out_bh)
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) */
152 int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
155 if (!buffer_uptodate(bh))
157 if (buffer_dirty(bh))
160 if (buffer_nilfs_node(bh) && nilfs_btree_broken_node_block(bh)) {
161 clear_buffer_uptodate(bh);
164 mark_buffer_dirty(bh);
168 int nilfs_init_gcinode(struct inode *inode)
170 struct nilfs_inode_info *ii = NILFS_I(inode);
172 inode->i_mode = S_IFREG;
173 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
174 inode->i_mapping->a_ops = &empty_aops;
175 inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
178 nilfs_bmap_init_gc(ii->i_bmap);
184 * nilfs_remove_all_gcinodes() - remove all unprocessed gc inodes
186 void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs)
188 struct list_head *head = &nilfs->ns_gc_inodes;
189 struct nilfs_inode_info *ii;
191 while (!list_empty(head)) {
192 ii = list_first_entry(head, struct nilfs_inode_info, i_dirty);
193 list_del_init(&ii->i_dirty);
194 iput(&ii->vfs_inode);