]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/nilfs2/gcinode.c
nilfs2: remove FSF mailing address from GPL notices
[mirror_ubuntu-zesty-kernel.git] / fs / nilfs2 / gcinode.c
CommitLineData
a3d93f70 1/*
047180f2 2 * gcinode.c - dummy inodes to buffer blocks for garbage collection
a3d93f70
RK
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 *
a3d93f70
RK
16 * Written by Seiji Kihara <kihara@osrg.net>, Amagai Yoshiji <amagai@osrg.net>,
17 * and Ryusuke Konishi <ryusuke@osrg.net>.
18 * Revised by Ryusuke Konishi <ryusuke@osrg.net>.
19 *
20 */
047180f2
RK
21/*
22 * This file adds the cache of on-disk blocks to be moved in garbage
23 * collection. The disk blocks are held with dummy inodes (called
24 * gcinodes), and this file provides lookup function of the dummy
25 * inodes and their buffer read function.
26 *
047180f2
RK
27 * Buffers and pages held by the dummy inodes will be released each
28 * time after they are copied to a new log. Dirty blocks made on the
29 * current generation and the blocks to be moved by GC never overlap
30 * because the dirty blocks make a new generation; they rather must be
31 * written individually.
32 */
a3d93f70
RK
33
34#include <linux/buffer_head.h>
35#include <linux/mpage.h>
36#include <linux/hash.h>
5a0e3ad6 37#include <linux/slab.h>
a3d93f70
RK
38#include <linux/swap.h>
39#include "nilfs.h"
05d0e94b
RK
40#include "btree.h"
41#include "btnode.h"
a3d93f70
RK
42#include "page.h"
43#include "mdt.h"
44#include "dat.h"
45#include "ifile.h"
46
a3d93f70
RK
47/*
48 * nilfs_gccache_submit_read_data() - add data buffer and submit read request
49 * @inode - gc inode
50 * @blkoff - dummy offset treated as the key for the page cache
51 * @pbn - physical block number of the block
52 * @vbn - virtual block number of the block, 0 for non-virtual block
53 * @out_bh - indirect pointer to a buffer_head struct to receive the results
54 *
55 * Description: nilfs_gccache_submit_read_data() registers the data buffer
56 * specified by @pbn to the GC pagecache with the key @blkoff.
57 * This function sets @vbn (@pbn if @vbn is zero) in b_blocknr of the buffer.
58 *
59 * Return Value: On success, 0 is returned. On Error, one of the following
60 * negative error code is returned.
61 *
62 * %-EIO - I/O error.
63 *
64 * %-ENOMEM - Insufficient amount of memory available.
65 *
66 * %-ENOENT - The block specified with @pbn does not exist.
67 */
68int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
69 sector_t pbn, __u64 vbn,
70 struct buffer_head **out_bh)
71{
72 struct buffer_head *bh;
73 int err;
74
75 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
76 if (unlikely(!bh))
77 return -ENOMEM;
78
79 if (buffer_uptodate(bh))
80 goto out;
81
82 if (pbn == 0) {
0ef28f9a
RK
83 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
84
85 err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
a3d93f70
RK
86 if (unlikely(err)) { /* -EIO, -ENOMEM, -ENOENT */
87 brelse(bh);
88 goto failed;
89 }
90 }
91
92 lock_buffer(bh);
93 if (buffer_uptodate(bh)) {
94 unlock_buffer(bh);
95 goto out;
96 }
97
98 if (!buffer_mapped(bh)) {
0ef28f9a 99 bh->b_bdev = inode->i_sb->s_bdev;
a3d93f70
RK
100 set_buffer_mapped(bh);
101 }
102 bh->b_blocknr = pbn;
103 bh->b_end_io = end_buffer_read_sync;
104 get_bh(bh);
105 submit_bh(READ, bh);
106 if (vbn)
107 bh->b_blocknr = vbn;
108 out:
109 err = 0;
110 *out_bh = bh;
111
112 failed:
113 unlock_page(bh->b_page);
09cbfeaf 114 put_page(bh->b_page);
a3d93f70
RK
115 return err;
116}
117
118/*
119 * nilfs_gccache_submit_read_node() - add node buffer and submit read request
120 * @inode - gc inode
121 * @pbn - physical block number for the block
122 * @vbn - virtual block number for the block
123 * @out_bh - indirect pointer to a buffer_head struct to receive the results
124 *
125 * Description: nilfs_gccache_submit_read_node() registers the node buffer
126 * specified by @vbn to the GC pagecache. @pbn can be supplied by the
127 * caller to avoid translation of the disk block address.
128 *
129 * Return Value: On success, 0 is returned. On Error, one of the following
130 * negative error code is returned.
131 *
132 * %-EIO - I/O error.
133 *
134 * %-ENOMEM - Insufficient amount of memory available.
135 */
136int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
137 __u64 vbn, struct buffer_head **out_bh)
138{
26dfdd8e
RK
139 int ret;
140
141 ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache,
142 vbn ? : pbn, pbn, READ, out_bh, &pbn);
a3d93f70
RK
143 if (ret == -EEXIST) /* internal code (cache hit) */
144 ret = 0;
145 return ret;
146}
147
148int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
149{
150 wait_on_buffer(bh);
151 if (!buffer_uptodate(bh))
152 return -EIO;
153 if (buffer_dirty(bh))
154 return -EEXIST;
155
5fc7b141
RK
156 if (buffer_nilfs_node(bh) && nilfs_btree_broken_node_block(bh)) {
157 clear_buffer_uptodate(bh);
158 return -EIO;
1d5385b9 159 }
5fc7b141 160 mark_buffer_dirty(bh);
a3d93f70
RK
161 return 0;
162}
163
263d90ce 164int nilfs_init_gcinode(struct inode *inode)
a3d93f70 165{
263d90ce 166 struct nilfs_inode_info *ii = NILFS_I(inode);
a3d93f70 167
adbb39b5
RK
168 inode->i_mode = S_IFREG;
169 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
293ce0ed 170 inode->i_mapping->a_ops = &empty_aops;
a3d93f70 171
adbb39b5
RK
172 ii->i_flags = 0;
173 nilfs_bmap_init_gc(ii->i_bmap);
a3d93f70 174
adbb39b5 175 return 0;
a3d93f70
RK
176}
177
263d90ce
RK
178/**
179 * nilfs_remove_all_gcinodes() - remove all unprocessed gc inodes
a3d93f70 180 */
263d90ce 181void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs)
a3d93f70 182{
263d90ce
RK
183 struct list_head *head = &nilfs->ns_gc_inodes;
184 struct nilfs_inode_info *ii;
a3d93f70 185
263d90ce
RK
186 while (!list_empty(head)) {
187 ii = list_first_entry(head, struct nilfs_inode_info, i_dirty);
188 list_del_init(&ii->i_dirty);
fbb24a3a
RK
189 truncate_inode_pages(&ii->vfs_inode.i_data, 0);
190 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
263d90ce 191 iput(&ii->vfs_inode);
a3d93f70
RK
192 }
193}