2 * gcinode.c - NILFS memory inode for GC
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 #include <linux/buffer_head.h>
27 #include <linux/mpage.h>
28 #include <linux/hash.h>
29 #include <linux/swap.h>
36 static struct address_space_operations def_gcinode_aops = {};
37 /* XXX need def_gcinode_iops/fops? */
40 * nilfs_gccache_submit_read_data() - add data buffer and submit read request
42 * @blkoff - dummy offset treated as the key for the page cache
43 * @pbn - physical block number of the block
44 * @vbn - virtual block number of the block, 0 for non-virtual block
45 * @out_bh - indirect pointer to a buffer_head struct to receive the results
47 * Description: nilfs_gccache_submit_read_data() registers the data buffer
48 * specified by @pbn to the GC pagecache with the key @blkoff.
49 * This function sets @vbn (@pbn if @vbn is zero) in b_blocknr of the buffer.
51 * Return Value: On success, 0 is returned. On Error, one of the following
52 * negative error code is returned.
56 * %-ENOMEM - Insufficient amount of memory available.
58 * %-ENOENT - The block specified with @pbn does not exist.
60 int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
61 sector_t pbn, __u64 vbn,
62 struct buffer_head **out_bh)
64 struct buffer_head *bh;
67 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
71 if (buffer_uptodate(bh))
75 struct inode *dat_inode = NILFS_I_NILFS(inode)->ns_dat;
76 /* use original dat, not gc dat. */
77 err = nilfs_dat_translate(dat_inode, vbn, &pbn);
78 if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
85 if (buffer_uptodate(bh)) {
90 if (!buffer_mapped(bh)) {
91 bh->b_bdev = NILFS_I_NILFS(inode)->ns_bdev;
92 set_buffer_mapped(bh);
95 bh->b_end_io = end_buffer_read_sync;
105 unlock_page(bh->b_page);
106 page_cache_release(bh->b_page);
111 * nilfs_gccache_submit_read_node() - add node buffer and submit read request
113 * @pbn - physical block number for the block
114 * @vbn - virtual block number for the block
115 * @out_bh - indirect pointer to a buffer_head struct to receive the results
117 * Description: nilfs_gccache_submit_read_node() registers the node buffer
118 * specified by @vbn to the GC pagecache. @pbn can be supplied by the
119 * caller to avoid translation of the disk block address.
121 * Return Value: On success, 0 is returned. On Error, one of the following
122 * negative error code is returned.
126 * %-ENOMEM - Insufficient amount of memory available.
128 int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
129 __u64 vbn, struct buffer_head **out_bh)
131 int ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache,
132 vbn ? : pbn, pbn, out_bh, 0);
133 if (ret == -EEXIST) /* internal code (cache hit) */
138 int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
141 if (!buffer_uptodate(bh))
143 if (buffer_dirty(bh))
146 if (buffer_nilfs_node(bh))
147 nilfs_btnode_mark_dirty(bh);
149 nilfs_mdt_mark_buffer_dirty(bh);
154 * nilfs_init_gccache() - allocate and initialize gc_inode hash table
157 * Return Value: On success, 0.
158 * On error, a negative error code is returned.
160 int nilfs_init_gccache(struct the_nilfs *nilfs)
164 BUG_ON(nilfs->ns_gc_inodes_h);
166 INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
168 nilfs->ns_gc_inodes_h =
169 kmalloc(sizeof(struct hlist_head) * NILFS_GCINODE_HASH_SIZE,
171 if (nilfs->ns_gc_inodes_h == NULL)
174 for (loop = 0; loop < NILFS_GCINODE_HASH_SIZE; loop++)
175 INIT_HLIST_HEAD(&nilfs->ns_gc_inodes_h[loop]);
180 * nilfs_destroy_gccache() - free gc_inode hash table
183 void nilfs_destroy_gccache(struct the_nilfs *nilfs)
185 if (nilfs->ns_gc_inodes_h) {
186 nilfs_remove_all_gcinode(nilfs);
187 kfree(nilfs->ns_gc_inodes_h);
188 nilfs->ns_gc_inodes_h = NULL;
192 static struct inode *alloc_gcinode(struct the_nilfs *nilfs, ino_t ino,
195 struct inode *inode = nilfs_mdt_new_common(nilfs, NULL, ino, GFP_NOFS);
196 struct nilfs_inode_info *ii;
203 inode->i_mapping->a_ops = &def_gcinode_aops;
208 ii->i_state = 1 << NILFS_I_GCINODE;
211 nilfs_bmap_init_gc(ii->i_bmap);
216 static unsigned long ihash(ino_t ino, __u64 cno)
218 return hash_long((unsigned long)((ino << 2) + cno),
219 NILFS_GCINODE_HASH_BITS);
223 * nilfs_gc_iget() - find or create gc inode with specified (ino,cno)
225 struct inode *nilfs_gc_iget(struct the_nilfs *nilfs, ino_t ino, __u64 cno)
227 struct hlist_head *head = nilfs->ns_gc_inodes_h + ihash(ino, cno);
228 struct hlist_node *node;
231 hlist_for_each_entry(inode, node, head, i_hash) {
232 if (inode->i_ino == ino && NILFS_I(inode)->i_cno == cno)
236 inode = alloc_gcinode(nilfs, ino, cno);
238 hlist_add_head(&inode->i_hash, head);
239 list_add(&NILFS_I(inode)->i_dirty, &nilfs->ns_gc_inodes);
245 * nilfs_clear_gcinode() - clear and free a gc inode
247 void nilfs_clear_gcinode(struct inode *inode)
249 nilfs_mdt_clear(inode);
250 nilfs_mdt_destroy(inode);
254 * nilfs_remove_all_gcinode() - remove all inodes from the_nilfs
256 void nilfs_remove_all_gcinode(struct the_nilfs *nilfs)
258 struct hlist_head *head = nilfs->ns_gc_inodes_h;
259 struct hlist_node *node, *n;
263 for (loop = 0; loop < NILFS_GCINODE_HASH_SIZE; loop++, head++) {
264 hlist_for_each_entry_safe(inode, node, n, head, i_hash) {
265 hlist_del_init(&inode->i_hash);
266 list_del_init(&NILFS_I(inode)->i_dirty);
267 nilfs_clear_gcinode(inode); /* might sleep */