1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/buffer_head.h>
34#include <linux/mpage.h>
35#include <linux/hash.h>
36#include <linux/slab.h>
37#include <linux/swap.h>
38#include "nilfs.h"
39#include "btree.h"
40#include "btnode.h"
41#include "page.h"
42#include "mdt.h"
43#include "dat.h"
44#include "ifile.h"
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
68 sector_t pbn, __u64 vbn,
69 struct buffer_head **out_bh)
70{
71 struct buffer_head *bh;
72 int err;
73
74 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
75 if (unlikely(!bh))
76 return -ENOMEM;
77
78 if (buffer_uptodate(bh))
79 goto out;
80
81 if (pbn == 0) {
82 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
83
84 err = nilfs_dat_translate(nilfs->ns_dat, vbn, &pbn);
85 if (unlikely(err)) {
86 brelse(bh);
87 goto failed;
88 }
89 }
90
91 lock_buffer(bh);
92 if (buffer_uptodate(bh)) {
93 unlock_buffer(bh);
94 goto out;
95 }
96
97 if (!buffer_mapped(bh)) {
98 bh->b_bdev = inode->i_sb->s_bdev;
99 set_buffer_mapped(bh);
100 }
101 bh->b_blocknr = pbn;
102 bh->b_end_io = end_buffer_read_sync;
103 get_bh(bh);
104 submit_bh(REQ_OP_READ, 0, bh);
105 if (vbn)
106 bh->b_blocknr = vbn;
107 out:
108 err = 0;
109 *out_bh = bh;
110
111 failed:
112 unlock_page(bh->b_page);
113 put_page(bh->b_page);
114 return err;
115}
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
136 __u64 vbn, struct buffer_head **out_bh)
137{
138 int ret;
139
140 ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache,
141 vbn ? : pbn, pbn, REQ_OP_READ, 0,
142 out_bh, &pbn);
143 if (ret == -EEXIST)
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 struct inode *inode = bh->b_page->mapping->host;
153
154 nilfs_msg(inode->i_sb, KERN_ERR,
155 "I/O error reading %s block for GC (ino=%lu, vblocknr=%llu)",
156 buffer_nilfs_node(bh) ? "node" : "data",
157 inode->i_ino, (unsigned long long)bh->b_blocknr);
158 return -EIO;
159 }
160 if (buffer_dirty(bh))
161 return -EEXIST;
162
163 if (buffer_nilfs_node(bh) && nilfs_btree_broken_node_block(bh)) {
164 clear_buffer_uptodate(bh);
165 return -EIO;
166 }
167 mark_buffer_dirty(bh);
168 return 0;
169}
170
171int nilfs_init_gcinode(struct inode *inode)
172{
173 struct nilfs_inode_info *ii = NILFS_I(inode);
174
175 inode->i_mode = S_IFREG;
176 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
177 inode->i_mapping->a_ops = &empty_aops;
178
179 ii->i_flags = 0;
180 nilfs_bmap_init_gc(ii->i_bmap);
181
182 return 0;
183}
184
185
186
187
188void nilfs_remove_all_gcinodes(struct the_nilfs *nilfs)
189{
190 struct list_head *head = &nilfs->ns_gc_inodes;
191 struct nilfs_inode_info *ii;
192
193 while (!list_empty(head)) {
194 ii = list_first_entry(head, struct nilfs_inode_info, i_dirty);
195 list_del_init(&ii->i_dirty);
196 truncate_inode_pages(&ii->vfs_inode.i_data, 0);
197 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
198 iput(&ii->vfs_inode);
199 }
200}
201