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
34
35
36
37
38
39
40
41
42
43
44
45#include <linux/buffer_head.h>
46#include <linux/mpage.h>
47#include <linux/hash.h>
48#include <linux/swap.h>
49#include "nilfs.h"
50#include "page.h"
51#include "mdt.h"
52#include "dat.h"
53#include "ifile.h"
54
55static const struct address_space_operations def_gcinode_aops = {
56 .sync_page = block_sync_page,
57};
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80int nilfs_gccache_submit_read_data(struct inode *inode, sector_t blkoff,
81 sector_t pbn, __u64 vbn,
82 struct buffer_head **out_bh)
83{
84 struct buffer_head *bh;
85 int err;
86
87 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0);
88 if (unlikely(!bh))
89 return -ENOMEM;
90
91 if (buffer_uptodate(bh))
92 goto out;
93
94 if (pbn == 0) {
95 struct inode *dat_inode = NILFS_I_NILFS(inode)->ns_dat;
96
97 err = nilfs_dat_translate(dat_inode, vbn, &pbn);
98 if (unlikely(err)) {
99 brelse(bh);
100 goto failed;
101 }
102 }
103
104 lock_buffer(bh);
105 if (buffer_uptodate(bh)) {
106 unlock_buffer(bh);
107 goto out;
108 }
109
110 if (!buffer_mapped(bh)) {
111 bh->b_bdev = NILFS_I_NILFS(inode)->ns_bdev;
112 set_buffer_mapped(bh);
113 }
114 bh->b_blocknr = pbn;
115 bh->b_end_io = end_buffer_read_sync;
116 get_bh(bh);
117 submit_bh(READ, bh);
118 if (vbn)
119 bh->b_blocknr = vbn;
120 out:
121 err = 0;
122 *out_bh = bh;
123
124 failed:
125 unlock_page(bh->b_page);
126 page_cache_release(bh->b_page);
127 return err;
128}
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148int nilfs_gccache_submit_read_node(struct inode *inode, sector_t pbn,
149 __u64 vbn, struct buffer_head **out_bh)
150{
151 int ret = nilfs_btnode_submit_block(&NILFS_I(inode)->i_btnode_cache,
152 vbn ? : pbn, pbn, out_bh, 0);
153 if (ret == -EEXIST)
154 ret = 0;
155 return ret;
156}
157
158int nilfs_gccache_wait_and_mark_dirty(struct buffer_head *bh)
159{
160 wait_on_buffer(bh);
161 if (!buffer_uptodate(bh))
162 return -EIO;
163 if (buffer_dirty(bh))
164 return -EEXIST;
165
166 if (buffer_nilfs_node(bh))
167 nilfs_btnode_mark_dirty(bh);
168 else
169 nilfs_mdt_mark_buffer_dirty(bh);
170 return 0;
171}
172
173
174
175
176
177
178
179
180int nilfs_init_gccache(struct the_nilfs *nilfs)
181{
182 int loop;
183
184 BUG_ON(nilfs->ns_gc_inodes_h);
185
186 INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
187
188 nilfs->ns_gc_inodes_h =
189 kmalloc(sizeof(struct hlist_head) * NILFS_GCINODE_HASH_SIZE,
190 GFP_NOFS);
191 if (nilfs->ns_gc_inodes_h == NULL)
192 return -ENOMEM;
193
194 for (loop = 0; loop < NILFS_GCINODE_HASH_SIZE; loop++)
195 INIT_HLIST_HEAD(&nilfs->ns_gc_inodes_h[loop]);
196 return 0;
197}
198
199
200
201
202
203void nilfs_destroy_gccache(struct the_nilfs *nilfs)
204{
205 if (nilfs->ns_gc_inodes_h) {
206 nilfs_remove_all_gcinode(nilfs);
207 kfree(nilfs->ns_gc_inodes_h);
208 nilfs->ns_gc_inodes_h = NULL;
209 }
210}
211
212static struct inode *alloc_gcinode(struct the_nilfs *nilfs, ino_t ino,
213 __u64 cno)
214{
215 struct inode *inode = nilfs_mdt_new_common(nilfs, NULL, ino, GFP_NOFS);
216 struct nilfs_inode_info *ii;
217
218 if (!inode)
219 return NULL;
220
221 inode->i_op = NULL;
222 inode->i_fop = NULL;
223 inode->i_mapping->a_ops = &def_gcinode_aops;
224
225 ii = NILFS_I(inode);
226 ii->i_cno = cno;
227 ii->i_flags = 0;
228 ii->i_state = 1 << NILFS_I_GCINODE;
229 ii->i_bh = NULL;
230 nilfs_bmap_init_gc(ii->i_bmap);
231
232 return inode;
233}
234
235static unsigned long ihash(ino_t ino, __u64 cno)
236{
237 return hash_long((unsigned long)((ino << 2) + cno),
238 NILFS_GCINODE_HASH_BITS);
239}
240
241
242
243
244struct inode *nilfs_gc_iget(struct the_nilfs *nilfs, ino_t ino, __u64 cno)
245{
246 struct hlist_head *head = nilfs->ns_gc_inodes_h + ihash(ino, cno);
247 struct hlist_node *node;
248 struct inode *inode;
249
250 hlist_for_each_entry(inode, node, head, i_hash) {
251 if (inode->i_ino == ino && NILFS_I(inode)->i_cno == cno)
252 return inode;
253 }
254
255 inode = alloc_gcinode(nilfs, ino, cno);
256 if (likely(inode)) {
257 hlist_add_head(&inode->i_hash, head);
258 list_add(&NILFS_I(inode)->i_dirty, &nilfs->ns_gc_inodes);
259 }
260 return inode;
261}
262
263
264
265
266void nilfs_clear_gcinode(struct inode *inode)
267{
268 nilfs_mdt_clear(inode);
269 nilfs_mdt_destroy(inode);
270}
271
272
273
274
275void nilfs_remove_all_gcinode(struct the_nilfs *nilfs)
276{
277 struct hlist_head *head = nilfs->ns_gc_inodes_h;
278 struct hlist_node *node, *n;
279 struct inode *inode;
280 int loop;
281
282 for (loop = 0; loop < NILFS_GCINODE_HASH_SIZE; loop++, head++) {
283 hlist_for_each_entry_safe(inode, node, n, head, i_hash) {
284 hlist_del_init(&inode->i_hash);
285 list_del_init(&NILFS_I(inode)->i_dirty);
286 nilfs_clear_gcinode(inode);
287 }
288 }
289}
290