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