Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 1 | /* |
Ryusuke Konishi | 047180f | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 2 | * gcinode.c - dummy inodes to buffer blocks for garbage collection |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 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 | */ |
Ryusuke Konishi | 047180f | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 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 | * |
Ryusuke Konishi | 047180f | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 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. |
| 36 | */ |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 37 | |
| 38 | #include <linux/buffer_head.h> |
| 39 | #include <linux/mpage.h> |
| 40 | #include <linux/hash.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 41 | #include <linux/slab.h> |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 42 | #include <linux/swap.h> |
| 43 | #include "nilfs.h" |
Ryusuke Konishi | 05d0e94 | 2010-07-10 20:52:09 +0900 | [diff] [blame] | 44 | #include "btree.h" |
| 45 | #include "btnode.h" |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 46 | #include "page.h" |
| 47 | #include "mdt.h" |
| 48 | #include "dat.h" |
| 49 | #include "ifile.h" |
| 50 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 51 | static const struct address_space_operations def_gcinode_aops = { |
Ryusuke Konishi | fa03274 | 2009-05-27 22:44:34 +0900 | [diff] [blame] | 52 | }; |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * nilfs_gccache_submit_read_data() - add data buffer and submit read request |
| 56 | * @inode - gc inode |
| 57 | * @blkoff - dummy offset treated as the key for the page cache |
| 58 | * @pbn - physical block number of the block |
| 59 | * @vbn - virtual block number of the block, 0 for non-virtual block |
| 60 | * @out_bh - indirect pointer to a buffer_head struct to receive the results |
| 61 | * |
| 62 | * Description: nilfs_gccache_submit_read_data() registers the data buffer |
| 63 | * specified by @pbn to the GC pagecache with the key @blkoff. |
| 64 | * This function sets @vbn (@pbn if @vbn is zero) in b_blocknr of the buffer. |
| 65 | * |
| 66 | * Return Value: On success, 0 is returned. On Error, one of the following |
| 67 | * negative error code is returned. |
| 68 | * |
| 69 | * %-EIO - I/O error. |
| 70 | * |
| 71 | * %-ENOMEM - Insufficient amount of memory available. |
| 72 | * |
| 73 | * %-ENOENT - The block specified with @pbn does not exist. |
| 74 | */ |
| 75 | int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff, |
| 76 | sector_t pbn, __u64 vbn, |
| 77 | struct buffer_head **out_bh) |
| 78 | { |
| 79 | struct buffer_head *bh; |
| 80 | int err; |
| 81 | |
| 82 | bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0); |
| 83 | if (unlikely(!bh)) |
| 84 | return -ENOMEM; |
| 85 | |
| 86 | if (buffer_uptodate(bh)) |
| 87 | goto out; |
| 88 | |
| 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 | |
| 99 | lock_buffer(bh); |
| 100 | if (buffer_uptodate(bh)) { |
| 101 | unlock_buffer(bh); |
| 102 | goto out; |
| 103 | } |
| 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 | |
| 119 | failed: |
| 120 | unlock_page(bh->b_page); |
| 121 | page_cache_release(bh->b_page); |
| 122 | return err; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * nilfs_gccache_submit_read_node() - add node buffer and submit read request |
| 127 | * @inode - gc inode |
| 128 | * @pbn - physical block number for the block |
| 129 | * @vbn - virtual block number for the block |
| 130 | * @out_bh - indirect pointer to a buffer_head struct to receive the results |
| 131 | * |
| 132 | * Description: nilfs_gccache_submit_read_node() registers the node buffer |
| 133 | * specified by @vbn to the GC pagecache. @pbn can be supplied by the |
| 134 | * caller to avoid translation of the disk block address. |
| 135 | * |
| 136 | * Return Value: On success, 0 is returned. On Error, one of the following |
| 137 | * negative error code is returned. |
| 138 | * |
| 139 | * %-EIO - I/O error. |
| 140 | * |
| 141 | * %-ENOMEM - Insufficient amount of memory available. |
| 142 | */ |
| 143 | int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn, |
| 144 | __u64 vbn, struct buffer_head **out_bh) |
| 145 | { |
Ryusuke Konishi | 26dfdd8 | 2010-07-18 10:42:23 +0900 | [diff] [blame] | 146 | int ret; |
| 147 | |
| 148 | ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache, |
| 149 | vbn ? : pbn, pbn, READ, out_bh, &pbn); |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 150 | if (ret == -EEXIST) /* internal code (cache hit) */ |
| 151 | ret = 0; |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh) |
| 156 | { |
| 157 | wait_on_buffer(bh); |
| 158 | if (!buffer_uptodate(bh)) |
| 159 | return -EIO; |
| 160 | if (buffer_dirty(bh)) |
| 161 | return -EEXIST; |
| 162 | |
Ryusuke Konishi | 1d5385b | 2010-07-16 23:52:40 +0900 | [diff] [blame] | 163 | if (buffer_nilfs_node(bh)) { |
| 164 | if (nilfs_btree_broken_node_block(bh)) { |
| 165 | clear_buffer_uptodate(bh); |
| 166 | return -EIO; |
| 167 | } |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 168 | nilfs_btnode_mark_dirty(bh); |
Ryusuke Konishi | 1d5385b | 2010-07-16 23:52:40 +0900 | [diff] [blame] | 169 | } else { |
Ryusuke Konishi | adbb39b | 2010-09-05 10:14:43 +0900 | [diff] [blame] | 170 | nilfs_mark_buffer_dirty(bh); |
Ryusuke Konishi | 1d5385b | 2010-07-16 23:52:40 +0900 | [diff] [blame] | 171 | } |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | |
Ryusuke Konishi | 263d90c | 2010-08-20 19:06:11 +0900 | [diff] [blame] | 175 | int nilfs_init_gcinode(struct inode *inode) |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 176 | { |
Ryusuke Konishi | 263d90c | 2010-08-20 19:06:11 +0900 | [diff] [blame] | 177 | struct nilfs_inode_info *ii = NILFS_I(inode); |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 178 | |
Ryusuke Konishi | adbb39b | 2010-09-05 10:14:43 +0900 | [diff] [blame] | 179 | inode->i_mode = S_IFREG; |
| 180 | mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); |
| 181 | inode->i_mapping->a_ops = &def_gcinode_aops; |
| 182 | inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi; |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 183 | |
Ryusuke Konishi | adbb39b | 2010-09-05 10:14:43 +0900 | [diff] [blame] | 184 | ii->i_flags = 0; |
| 185 | nilfs_bmap_init_gc(ii->i_bmap); |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 186 | |
Ryusuke Konishi | adbb39b | 2010-09-05 10:14:43 +0900 | [diff] [blame] | 187 | return 0; |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Ryusuke Konishi | 263d90c | 2010-08-20 19:06:11 +0900 | [diff] [blame] | 190 | /** |
| 191 | * nilfs_remove_all_gcinodes() - remove all unprocessed gc inodes |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 192 | */ |
Ryusuke Konishi | 263d90c | 2010-08-20 19:06:11 +0900 | [diff] [blame] | 193 | void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs) |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 194 | { |
Ryusuke Konishi | 263d90c | 2010-08-20 19:06:11 +0900 | [diff] [blame] | 195 | struct list_head *head = &nilfs->ns_gc_inodes; |
| 196 | struct nilfs_inode_info *ii; |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 197 | |
Ryusuke Konishi | 263d90c | 2010-08-20 19:06:11 +0900 | [diff] [blame] | 198 | while (!list_empty(head)) { |
| 199 | ii = list_first_entry(head, struct nilfs_inode_info, i_dirty); |
| 200 | list_del_init(&ii->i_dirty); |
| 201 | iput(&ii->vfs_inode); |
Ryusuke Konishi | a3d93f7 | 2009-04-06 19:01:40 -0700 | [diff] [blame] | 202 | } |
| 203 | } |