blob: 33ad25ddd5c442a7875e8eca9b078b363e10d9ce [file] [log] [blame]
Ryusuke Konishia3d93f72009-04-06 19:01:40 -07001/*
Ryusuke Konishi047180f2009-04-06 19:01:45 -07002 * gcinode.c - dummy inodes to buffer blocks for garbage collection
Ryusuke Konishia3d93f72009-04-06 19:01:40 -07003 *
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 Konishi047180f2009-04-06 19:01:45 -070025/*
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 Konishi047180f2009-04-06 19:01:45 -070031 * 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 Konishia3d93f72009-04-06 19:01:40 -070037
38#include <linux/buffer_head.h>
39#include <linux/mpage.h>
40#include <linux/hash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Ryusuke Konishia3d93f72009-04-06 19:01:40 -070042#include <linux/swap.h>
43#include "nilfs.h"
Ryusuke Konishi05d0e942010-07-10 20:52:09 +090044#include "btree.h"
45#include "btnode.h"
Ryusuke Konishia3d93f72009-04-06 19:01:40 -070046#include "page.h"
47#include "mdt.h"
48#include "dat.h"
49#include "ifile.h"
50
Alexey Dobriyan7f094102009-09-21 17:01:10 -070051static const struct address_space_operations def_gcinode_aops = {
Ryusuke Konishifa032742009-05-27 22:44:34 +090052 .sync_page = block_sync_page,
53};
Ryusuke Konishia3d93f72009-04-06 19:01:40 -070054
55/*
56 * nilfs_gccache_submit_read_data() - add data buffer and submit read request
57 * @inode - gc inode
58 * @blkoff - dummy offset treated as the key for the page cache
59 * @pbn - physical block number of the block
60 * @vbn - virtual block number of the block, 0 for non-virtual block
61 * @out_bh - indirect pointer to a buffer_head struct to receive the results
62 *
63 * Description: nilfs_gccache_submit_read_data() registers the data buffer
64 * specified by @pbn to the GC pagecache with the key @blkoff.
65 * This function sets @vbn (@pbn if @vbn is zero) in b_blocknr of the buffer.
66 *
67 * Return Value: On success, 0 is returned. On Error, one of the following
68 * negative error code is returned.
69 *
70 * %-EIO - I/O error.
71 *
72 * %-ENOMEM - Insufficient amount of memory available.
73 *
74 * %-ENOENT - The block specified with @pbn does not exist.
75 */
76int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
77 sector_t pbn, __u64 vbn,
78 struct buffer_head **out_bh)
79{
80 struct buffer_head *bh;
81 int err;
82
83 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
84 if (unlikely(!bh))
85 return -ENOMEM;
86
87 if (buffer_uptodate(bh))
88 goto out;
89
90 if (pbn == 0) {
91 struct inode *dat_inode = NILFS_I_NILFS(inode)->ns_dat;
92 /* use original dat, not gc dat. */
93 err = nilfs_dat_translate(dat_inode, vbn, &pbn);
94 if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
95 brelse(bh);
96 goto failed;
97 }
98 }
99
100 lock_buffer(bh);
101 if (buffer_uptodate(bh)) {
102 unlock_buffer(bh);
103 goto out;
104 }
105
106 if (!buffer_mapped(bh)) {
107 bh->b_bdev = NILFS_I_NILFS(inode)->ns_bdev;
108 set_buffer_mapped(bh);
109 }
110 bh->b_blocknr = pbn;
111 bh->b_end_io = end_buffer_read_sync;
112 get_bh(bh);
113 submit_bh(READ, bh);
114 if (vbn)
115 bh->b_blocknr = vbn;
116 out:
117 err = 0;
118 *out_bh = bh;
119
120 failed:
121 unlock_page(bh->b_page);
122 page_cache_release(bh->b_page);
123 return err;
124}
125
126/*
127 * nilfs_gccache_submit_read_node() - add node buffer and submit read request
128 * @inode - gc inode
129 * @pbn - physical block number for the block
130 * @vbn - virtual block number for the block
131 * @out_bh - indirect pointer to a buffer_head struct to receive the results
132 *
133 * Description: nilfs_gccache_submit_read_node() registers the node buffer
134 * specified by @vbn to the GC pagecache. @pbn can be supplied by the
135 * caller to avoid translation of the disk block address.
136 *
137 * Return Value: On success, 0 is returned. On Error, one of the following
138 * negative error code is returned.
139 *
140 * %-EIO - I/O error.
141 *
142 * %-ENOMEM - Insufficient amount of memory available.
143 */
144int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
145 __u64 vbn, struct buffer_head **out_bh)
146{
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +0900147 int ret;
148
149 ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache,
150 vbn ? : pbn, pbn, READ, out_bh, &pbn);
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700151 if (ret == -EEXIST) /* internal code (cache hit) */
152 ret = 0;
153 return ret;
154}
155
156int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
157{
158 wait_on_buffer(bh);
159 if (!buffer_uptodate(bh))
160 return -EIO;
161 if (buffer_dirty(bh))
162 return -EEXIST;
163
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900164 if (buffer_nilfs_node(bh)) {
165 if (nilfs_btree_broken_node_block(bh)) {
166 clear_buffer_uptodate(bh);
167 return -EIO;
168 }
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700169 nilfs_btnode_mark_dirty(bh);
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900170 } else {
Ryusuke Konishiadbb39b2010-09-05 10:14:43 +0900171 nilfs_mark_buffer_dirty(bh);
Ryusuke Konishi1d5385b2010-07-16 23:52:40 +0900172 }
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700173 return 0;
174}
175
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900176int nilfs_init_gcinode(struct inode *inode)
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700177{
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900178 struct nilfs_inode_info *ii = NILFS_I(inode);
179 struct the_nilfs *nilfs = NILFS_SB(inode->i_sb)->s_nilfs;
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700180
Ryusuke Konishiadbb39b2010-09-05 10:14:43 +0900181 inode->i_mode = S_IFREG;
182 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
183 inode->i_mapping->a_ops = &def_gcinode_aops;
184 inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700185
Ryusuke Konishiadbb39b2010-09-05 10:14:43 +0900186 ii->i_flags = 0;
187 nilfs_bmap_init_gc(ii->i_bmap);
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700188
Ryusuke Konishiadbb39b2010-09-05 10:14:43 +0900189 /*
190 * Add the inode to GC inode list. Garbage Collection
191 * is serialized and no two processes manipulate the
192 * list simultaneously.
193 */
194 igrab(inode);
195 list_add(&NILFS_I(inode)->i_dirty, &nilfs->ns_gc_inodes);
196
197 return 0;
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700198}
199
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900200/**
201 * nilfs_remove_all_gcinodes() - remove all unprocessed gc inodes
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700202 */
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900203void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs)
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700204{
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900205 struct list_head *head = &nilfs->ns_gc_inodes;
206 struct nilfs_inode_info *ii;
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700207
Ryusuke Konishi263d90c2010-08-20 19:06:11 +0900208 while (!list_empty(head)) {
209 ii = list_first_entry(head, struct nilfs_inode_info, i_dirty);
210 list_del_init(&ii->i_dirty);
211 iput(&ii->vfs_inode);
Ryusuke Konishia3d93f72009-04-06 19:01:40 -0700212 }
213}