diff options
author | Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp> | 2009-04-06 22:01:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-04-07 11:31:15 -0400 |
commit | 64b5a32e0b3680a9655b3f2e668a646068e71d33 (patch) | |
tree | f4b97cde4f593a55fef4f754ea1e16a28cdf9f38 /fs | |
parent | 783f61843e9d14724f5f79ae955a28c27e300467 (diff) |
nilfs2: segment buffer
This adds the segment buffer which is used to constuct logs.
[akpm@linux-foundation.org: BIO_RW_SYNC got removed]
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/nilfs2/segbuf.c | 461 | ||||
-rw-r--r-- | fs/nilfs2/segbuf.h | 203 |
2 files changed, 664 insertions, 0 deletions
diff --git a/fs/nilfs2/segbuf.c b/fs/nilfs2/segbuf.c new file mode 100644 index 000000000000..3d3ea8351f6d --- /dev/null +++ b/fs/nilfs2/segbuf.c | |||
@@ -0,0 +1,461 @@ | |||
1 | /* | ||
2 | * segbuf.c - NILFS segment buffer | ||
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 Ryusuke Konishi <ryusuke@osrg.net> | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include <linux/buffer_head.h> | ||
25 | #include <linux/writeback.h> | ||
26 | #include <linux/crc32.h> | ||
27 | #include "page.h" | ||
28 | #include "segbuf.h" | ||
29 | #include "seglist.h" | ||
30 | |||
31 | |||
32 | static struct kmem_cache *nilfs_segbuf_cachep; | ||
33 | |||
34 | static void nilfs_segbuf_init_once(void *obj) | ||
35 | { | ||
36 | memset(obj, 0, sizeof(struct nilfs_segment_buffer)); | ||
37 | } | ||
38 | |||
39 | int __init nilfs_init_segbuf_cache(void) | ||
40 | { | ||
41 | nilfs_segbuf_cachep = | ||
42 | kmem_cache_create("nilfs2_segbuf_cache", | ||
43 | sizeof(struct nilfs_segment_buffer), | ||
44 | 0, SLAB_RECLAIM_ACCOUNT, | ||
45 | nilfs_segbuf_init_once); | ||
46 | |||
47 | return (nilfs_segbuf_cachep == NULL) ? -ENOMEM : 0; | ||
48 | } | ||
49 | |||
50 | void nilfs_destroy_segbuf_cache(void) | ||
51 | { | ||
52 | kmem_cache_destroy(nilfs_segbuf_cachep); | ||
53 | } | ||
54 | |||
55 | struct nilfs_segment_buffer *nilfs_segbuf_new(struct super_block *sb) | ||
56 | { | ||
57 | struct nilfs_segment_buffer *segbuf; | ||
58 | |||
59 | segbuf = kmem_cache_alloc(nilfs_segbuf_cachep, GFP_NOFS); | ||
60 | if (unlikely(!segbuf)) | ||
61 | return NULL; | ||
62 | |||
63 | segbuf->sb_super = sb; | ||
64 | INIT_LIST_HEAD(&segbuf->sb_list); | ||
65 | INIT_LIST_HEAD(&segbuf->sb_segsum_buffers); | ||
66 | INIT_LIST_HEAD(&segbuf->sb_payload_buffers); | ||
67 | segbuf->sb_segent = NULL; | ||
68 | return segbuf; | ||
69 | } | ||
70 | |||
71 | void nilfs_segbuf_free(struct nilfs_segment_buffer *segbuf) | ||
72 | { | ||
73 | struct nilfs_segment_entry *ent = segbuf->sb_segent; | ||
74 | |||
75 | if (ent != NULL && list_empty(&ent->list)) { | ||
76 | /* free isolated segment list head */ | ||
77 | nilfs_free_segment_entry(segbuf->sb_segent); | ||
78 | segbuf->sb_segent = NULL; | ||
79 | } | ||
80 | kmem_cache_free(nilfs_segbuf_cachep, segbuf); | ||
81 | } | ||
82 | |||
83 | int nilfs_segbuf_map(struct nilfs_segment_buffer *segbuf, __u64 segnum, | ||
84 | unsigned long offset, struct the_nilfs *nilfs) | ||
85 | { | ||
86 | struct nilfs_segment_entry *ent; | ||
87 | |||
88 | segbuf->sb_segnum = segnum; | ||
89 | nilfs_get_segment_range(nilfs, segnum, &segbuf->sb_fseg_start, | ||
90 | &segbuf->sb_fseg_end); | ||
91 | |||
92 | segbuf->sb_pseg_start = segbuf->sb_fseg_start + offset; | ||
93 | segbuf->sb_rest_blocks = | ||
94 | segbuf->sb_fseg_end - segbuf->sb_pseg_start + 1; | ||
95 | |||
96 | /* Attach a segment list head */ | ||
97 | ent = segbuf->sb_segent; | ||
98 | if (ent == NULL) { | ||
99 | segbuf->sb_segent = nilfs_alloc_segment_entry(segnum); | ||
100 | if (unlikely(!segbuf->sb_segent)) | ||
101 | return -ENOMEM; | ||
102 | } else { | ||
103 | BUG_ON(ent->bh_su || !list_empty(&ent->list)); | ||
104 | ent->segnum = segnum; | ||
105 | } | ||
106 | return 0; | ||
107 | } | ||
108 | |||
109 | void nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer *segbuf, | ||
110 | __u64 nextnum, struct the_nilfs *nilfs) | ||
111 | { | ||
112 | segbuf->sb_nextnum = nextnum; | ||
113 | segbuf->sb_sum.next = nilfs_get_segment_start_blocknr(nilfs, nextnum); | ||
114 | } | ||
115 | |||
116 | int nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer *segbuf) | ||
117 | { | ||
118 | struct buffer_head *bh; | ||
119 | |||
120 | bh = sb_getblk(segbuf->sb_super, | ||
121 | segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk); | ||
122 | if (unlikely(!bh)) | ||
123 | return -ENOMEM; | ||
124 | |||
125 | nilfs_segbuf_add_segsum_buffer(segbuf, bh); | ||
126 | return 0; | ||
127 | } | ||
128 | |||
129 | int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer *segbuf, | ||
130 | struct buffer_head **bhp) | ||
131 | { | ||
132 | struct buffer_head *bh; | ||
133 | |||
134 | bh = sb_getblk(segbuf->sb_super, | ||
135 | segbuf->sb_pseg_start + segbuf->sb_sum.nblocks); | ||
136 | if (unlikely(!bh)) | ||
137 | return -ENOMEM; | ||
138 | |||
139 | nilfs_segbuf_add_payload_buffer(segbuf, bh); | ||
140 | *bhp = bh; | ||
141 | return 0; | ||
142 | } | ||
143 | |||
144 | int nilfs_segbuf_reset(struct nilfs_segment_buffer *segbuf, unsigned flags, | ||
145 | time_t ctime) | ||
146 | { | ||
147 | int err; | ||
148 | |||
149 | segbuf->sb_sum.nblocks = segbuf->sb_sum.nsumblk = 0; | ||
150 | err = nilfs_segbuf_extend_segsum(segbuf); | ||
151 | if (unlikely(err)) | ||
152 | return err; | ||
153 | |||
154 | segbuf->sb_sum.flags = flags; | ||
155 | segbuf->sb_sum.sumbytes = sizeof(struct nilfs_segment_summary); | ||
156 | segbuf->sb_sum.nfinfo = segbuf->sb_sum.nfileblk = 0; | ||
157 | segbuf->sb_sum.ctime = ctime; | ||
158 | |||
159 | segbuf->sb_io_error = 0; | ||
160 | return 0; | ||
161 | } | ||
162 | |||
163 | /* | ||
164 | * Setup segument summary | ||
165 | */ | ||
166 | void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer *segbuf) | ||
167 | { | ||
168 | struct nilfs_segment_summary *raw_sum; | ||
169 | struct buffer_head *bh_sum; | ||
170 | |||
171 | bh_sum = list_entry(segbuf->sb_segsum_buffers.next, | ||
172 | struct buffer_head, b_assoc_buffers); | ||
173 | raw_sum = (struct nilfs_segment_summary *)bh_sum->b_data; | ||
174 | |||
175 | raw_sum->ss_magic = cpu_to_le32(NILFS_SEGSUM_MAGIC); | ||
176 | raw_sum->ss_bytes = cpu_to_le16(sizeof(*raw_sum)); | ||
177 | raw_sum->ss_flags = cpu_to_le16(segbuf->sb_sum.flags); | ||
178 | raw_sum->ss_seq = cpu_to_le64(segbuf->sb_sum.seg_seq); | ||
179 | raw_sum->ss_create = cpu_to_le64(segbuf->sb_sum.ctime); | ||
180 | raw_sum->ss_next = cpu_to_le64(segbuf->sb_sum.next); | ||
181 | raw_sum->ss_nblocks = cpu_to_le32(segbuf->sb_sum.nblocks); | ||
182 | raw_sum->ss_nfinfo = cpu_to_le32(segbuf->sb_sum.nfinfo); | ||
183 | raw_sum->ss_sumbytes = cpu_to_le32(segbuf->sb_sum.sumbytes); | ||
184 | raw_sum->ss_pad = 0; | ||
185 | } | ||
186 | |||
187 | /* | ||
188 | * CRC calculation routines | ||
189 | */ | ||
190 | void nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer *segbuf, | ||
191 | u32 seed) | ||
192 | { | ||
193 | struct buffer_head *bh; | ||
194 | struct nilfs_segment_summary *raw_sum; | ||
195 | unsigned long size, bytes = segbuf->sb_sum.sumbytes; | ||
196 | u32 crc; | ||
197 | |||
198 | bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head, | ||
199 | b_assoc_buffers); | ||
200 | |||
201 | raw_sum = (struct nilfs_segment_summary *)bh->b_data; | ||
202 | size = min_t(unsigned long, bytes, bh->b_size); | ||
203 | crc = crc32_le(seed, | ||
204 | (unsigned char *)raw_sum + | ||
205 | sizeof(raw_sum->ss_datasum) + sizeof(raw_sum->ss_sumsum), | ||
206 | size - (sizeof(raw_sum->ss_datasum) + | ||
207 | sizeof(raw_sum->ss_sumsum))); | ||
208 | |||
209 | list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers, | ||
210 | b_assoc_buffers) { | ||
211 | bytes -= size; | ||
212 | size = min_t(unsigned long, bytes, bh->b_size); | ||
213 | crc = crc32_le(crc, bh->b_data, size); | ||
214 | } | ||
215 | raw_sum->ss_sumsum = cpu_to_le32(crc); | ||
216 | } | ||
217 | |||
218 | void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer *segbuf, | ||
219 | u32 seed) | ||
220 | { | ||
221 | struct buffer_head *bh; | ||
222 | struct nilfs_segment_summary *raw_sum; | ||
223 | void *kaddr; | ||
224 | u32 crc; | ||
225 | |||
226 | bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head, | ||
227 | b_assoc_buffers); | ||
228 | raw_sum = (struct nilfs_segment_summary *)bh->b_data; | ||
229 | crc = crc32_le(seed, | ||
230 | (unsigned char *)raw_sum + sizeof(raw_sum->ss_datasum), | ||
231 | bh->b_size - sizeof(raw_sum->ss_datasum)); | ||
232 | |||
233 | list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers, | ||
234 | b_assoc_buffers) { | ||
235 | crc = crc32_le(crc, bh->b_data, bh->b_size); | ||
236 | } | ||
237 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) { | ||
238 | kaddr = kmap_atomic(bh->b_page, KM_USER0); | ||
239 | crc = crc32_le(crc, kaddr + bh_offset(bh), bh->b_size); | ||
240 | kunmap_atomic(kaddr, KM_USER0); | ||
241 | } | ||
242 | raw_sum->ss_datasum = cpu_to_le32(crc); | ||
243 | } | ||
244 | |||
245 | void nilfs_release_buffers(struct list_head *list) | ||
246 | { | ||
247 | struct buffer_head *bh, *n; | ||
248 | |||
249 | list_for_each_entry_safe(bh, n, list, b_assoc_buffers) { | ||
250 | list_del_init(&bh->b_assoc_buffers); | ||
251 | if (buffer_nilfs_allocated(bh)) { | ||
252 | struct page *clone_page = bh->b_page; | ||
253 | |||
254 | /* remove clone page */ | ||
255 | brelse(bh); | ||
256 | page_cache_release(clone_page); /* for each bh */ | ||
257 | if (page_count(clone_page) <= 2) { | ||
258 | lock_page(clone_page); | ||
259 | nilfs_free_private_page(clone_page); | ||
260 | } | ||
261 | continue; | ||
262 | } | ||
263 | brelse(bh); | ||
264 | } | ||
265 | } | ||
266 | |||
267 | /* | ||
268 | * BIO operations | ||
269 | */ | ||
270 | static void nilfs_end_bio_write(struct bio *bio, int err) | ||
271 | { | ||
272 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | ||
273 | struct nilfs_write_info *wi = bio->bi_private; | ||
274 | |||
275 | if (err == -EOPNOTSUPP) { | ||
276 | set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); | ||
277 | bio_put(bio); | ||
278 | /* to be detected by submit_seg_bio() */ | ||
279 | } | ||
280 | |||
281 | if (!uptodate) | ||
282 | atomic_inc(&wi->err); | ||
283 | |||
284 | bio_put(bio); | ||
285 | complete(&wi->bio_event); | ||
286 | } | ||
287 | |||
288 | static int nilfs_submit_seg_bio(struct nilfs_write_info *wi, int mode) | ||
289 | { | ||
290 | struct bio *bio = wi->bio; | ||
291 | int err; | ||
292 | |||
293 | if (wi->nbio > 0 && bdi_write_congested(wi->bdi)) { | ||
294 | wait_for_completion(&wi->bio_event); | ||
295 | wi->nbio--; | ||
296 | if (unlikely(atomic_read(&wi->err))) { | ||
297 | bio_put(bio); | ||
298 | err = -EIO; | ||
299 | goto failed; | ||
300 | } | ||
301 | } | ||
302 | |||
303 | bio->bi_end_io = nilfs_end_bio_write; | ||
304 | bio->bi_private = wi; | ||
305 | bio_get(bio); | ||
306 | submit_bio(mode, bio); | ||
307 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) { | ||
308 | bio_put(bio); | ||
309 | err = -EOPNOTSUPP; | ||
310 | goto failed; | ||
311 | } | ||
312 | wi->nbio++; | ||
313 | bio_put(bio); | ||
314 | |||
315 | wi->bio = NULL; | ||
316 | wi->rest_blocks -= wi->end - wi->start; | ||
317 | wi->nr_vecs = min(wi->max_pages, wi->rest_blocks); | ||
318 | wi->start = wi->end; | ||
319 | return 0; | ||
320 | |||
321 | failed: | ||
322 | wi->bio = NULL; | ||
323 | return err; | ||
324 | } | ||
325 | |||
326 | /** | ||
327 | * nilfs_alloc_seg_bio - allocate a bio for writing segment. | ||
328 | * @sb: super block | ||
329 | * @start: beginning disk block number of this BIO. | ||
330 | * @nr_vecs: request size of page vector. | ||
331 | * | ||
332 | * alloc_seg_bio() allocates a new BIO structure and initialize it. | ||
333 | * | ||
334 | * Return Value: On success, pointer to the struct bio is returned. | ||
335 | * On error, NULL is returned. | ||
336 | */ | ||
337 | static struct bio *nilfs_alloc_seg_bio(struct super_block *sb, sector_t start, | ||
338 | int nr_vecs) | ||
339 | { | ||
340 | struct bio *bio; | ||
341 | |||
342 | bio = bio_alloc(GFP_NOWAIT, nr_vecs); | ||
343 | if (bio == NULL) { | ||
344 | while (!bio && (nr_vecs >>= 1)) | ||
345 | bio = bio_alloc(GFP_NOWAIT, nr_vecs); | ||
346 | } | ||
347 | if (likely(bio)) { | ||
348 | bio->bi_bdev = sb->s_bdev; | ||
349 | bio->bi_sector = (sector_t)start << (sb->s_blocksize_bits - 9); | ||
350 | } | ||
351 | return bio; | ||
352 | } | ||
353 | |||
354 | void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf, | ||
355 | struct nilfs_write_info *wi) | ||
356 | { | ||
357 | wi->bio = NULL; | ||
358 | wi->rest_blocks = segbuf->sb_sum.nblocks; | ||
359 | wi->max_pages = bio_get_nr_vecs(wi->sb->s_bdev); | ||
360 | wi->nr_vecs = min(wi->max_pages, wi->rest_blocks); | ||
361 | wi->start = wi->end = 0; | ||
362 | wi->nbio = 0; | ||
363 | wi->blocknr = segbuf->sb_pseg_start; | ||
364 | |||
365 | atomic_set(&wi->err, 0); | ||
366 | init_completion(&wi->bio_event); | ||
367 | } | ||
368 | |||
369 | static int nilfs_submit_bh(struct nilfs_write_info *wi, struct buffer_head *bh, | ||
370 | int mode) | ||
371 | { | ||
372 | int len, err; | ||
373 | |||
374 | BUG_ON(wi->nr_vecs <= 0); | ||
375 | repeat: | ||
376 | if (!wi->bio) { | ||
377 | wi->bio = nilfs_alloc_seg_bio(wi->sb, wi->blocknr + wi->end, | ||
378 | wi->nr_vecs); | ||
379 | if (unlikely(!wi->bio)) | ||
380 | return -ENOMEM; | ||
381 | } | ||
382 | |||
383 | len = bio_add_page(wi->bio, bh->b_page, bh->b_size, bh_offset(bh)); | ||
384 | if (len == bh->b_size) { | ||
385 | wi->end++; | ||
386 | return 0; | ||
387 | } | ||
388 | /* bio is FULL */ | ||
389 | err = nilfs_submit_seg_bio(wi, mode); | ||
390 | /* never submit current bh */ | ||
391 | if (likely(!err)) | ||
392 | goto repeat; | ||
393 | return err; | ||
394 | } | ||
395 | |||
396 | int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf, | ||
397 | struct nilfs_write_info *wi) | ||
398 | { | ||
399 | struct buffer_head *bh; | ||
400 | int res, rw = WRITE; | ||
401 | |||
402 | list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) { | ||
403 | res = nilfs_submit_bh(wi, bh, rw); | ||
404 | if (unlikely(res)) | ||
405 | goto failed_bio; | ||
406 | } | ||
407 | |||
408 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) { | ||
409 | res = nilfs_submit_bh(wi, bh, rw); | ||
410 | if (unlikely(res)) | ||
411 | goto failed_bio; | ||
412 | } | ||
413 | |||
414 | if (wi->bio) { | ||
415 | /* | ||
416 | * Last BIO is always sent through the following | ||
417 | * submission. | ||
418 | */ | ||
419 | rw |= (1 << BIO_RW_SYNCIO); | ||
420 | res = nilfs_submit_seg_bio(wi, rw); | ||
421 | if (unlikely(res)) | ||
422 | goto failed_bio; | ||
423 | } | ||
424 | |||
425 | res = 0; | ||
426 | out: | ||
427 | return res; | ||
428 | |||
429 | failed_bio: | ||
430 | atomic_inc(&wi->err); | ||
431 | goto out; | ||
432 | } | ||
433 | |||
434 | /** | ||
435 | * nilfs_segbuf_wait - wait for completion of requested BIOs | ||
436 | * @wi: nilfs_write_info | ||
437 | * | ||
438 | * Return Value: On Success, 0 is returned. On Error, one of the following | ||
439 | * negative error code is returned. | ||
440 | * | ||
441 | * %-EIO - I/O error | ||
442 | */ | ||
443 | int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf, | ||
444 | struct nilfs_write_info *wi) | ||
445 | { | ||
446 | int err = 0; | ||
447 | |||
448 | if (!wi->nbio) | ||
449 | return 0; | ||
450 | |||
451 | do { | ||
452 | wait_for_completion(&wi->bio_event); | ||
453 | } while (--wi->nbio > 0); | ||
454 | |||
455 | if (unlikely(atomic_read(&wi->err) > 0)) { | ||
456 | printk(KERN_ERR "NILFS: IO error writing segment\n"); | ||
457 | err = -EIO; | ||
458 | segbuf->sb_io_error = 1; | ||
459 | } | ||
460 | return err; | ||
461 | } | ||
diff --git a/fs/nilfs2/segbuf.h b/fs/nilfs2/segbuf.h new file mode 100644 index 000000000000..25f2a5faa483 --- /dev/null +++ b/fs/nilfs2/segbuf.h | |||
@@ -0,0 +1,203 @@ | |||
1 | /* | ||
2 | * segbuf.h - NILFS Segment buffer prototypes and definitions | ||
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 Ryusuke Konishi <ryusuke@osrg.net> | ||
21 | * | ||
22 | */ | ||
23 | #ifndef _NILFS_SEGBUF_H | ||
24 | #define _NILFS_SEGBUF_H | ||
25 | |||
26 | #include <linux/fs.h> | ||
27 | #include <linux/buffer_head.h> | ||
28 | #include <linux/bio.h> | ||
29 | #include <linux/completion.h> | ||
30 | #include <linux/backing-dev.h> | ||
31 | |||
32 | /** | ||
33 | * struct nilfs_segsum_info - On-memory segment summary | ||
34 | * @flags: Flags | ||
35 | * @nfinfo: Number of file information structures | ||
36 | * @nblocks: Number of blocks included in the partial segment | ||
37 | * @nsumblk: Number of summary blocks | ||
38 | * @sumbytes: Byte count of segment summary | ||
39 | * @nfileblk: Total number of file blocks | ||
40 | * @seg_seq: Segment sequence number | ||
41 | * @ctime: Creation time | ||
42 | * @next: Block number of the next full segment | ||
43 | */ | ||
44 | struct nilfs_segsum_info { | ||
45 | unsigned int flags; | ||
46 | unsigned long nfinfo; | ||
47 | unsigned long nblocks; | ||
48 | unsigned long nsumblk; | ||
49 | unsigned long sumbytes; | ||
50 | unsigned long nfileblk; | ||
51 | u64 seg_seq; | ||
52 | time_t ctime; | ||
53 | sector_t next; | ||
54 | }; | ||
55 | |||
56 | /* macro for the flags */ | ||
57 | #define NILFS_SEG_HAS_SR(sum) ((sum)->flags & NILFS_SS_SR) | ||
58 | #define NILFS_SEG_LOGBGN(sum) ((sum)->flags & NILFS_SS_LOGBGN) | ||
59 | #define NILFS_SEG_LOGEND(sum) ((sum)->flags & NILFS_SS_LOGEND) | ||
60 | #define NILFS_SEG_DSYNC(sum) ((sum)->flags & NILFS_SS_SYNDT) | ||
61 | #define NILFS_SEG_SIMPLEX(sum) \ | ||
62 | (((sum)->flags & (NILFS_SS_LOGBGN | NILFS_SS_LOGEND)) == \ | ||
63 | (NILFS_SS_LOGBGN | NILFS_SS_LOGEND)) | ||
64 | |||
65 | #define NILFS_SEG_EMPTY(sum) ((sum)->nblocks == (sum)->nsumblk) | ||
66 | |||
67 | /** | ||
68 | * struct nilfs_segment_buffer - Segment buffer | ||
69 | * @sb_super: back pointer to a superblock struct | ||
70 | * @sb_list: List head to chain this structure | ||
71 | * @sb_segent: Pointer for attaching a segment entry | ||
72 | * @sb_sum: On-memory segment summary | ||
73 | * @sb_segnum: Index number of the full segment | ||
74 | * @sb_nextnum: Index number of the next full segment | ||
75 | * @sb_fseg_start: Start block number of the full segment | ||
76 | * @sb_fseg_end: End block number of the full segment | ||
77 | * @sb_pseg_start: Disk block number of partial segment | ||
78 | * @sb_rest_blocks: Number of residual blocks in the current segment | ||
79 | * @sb_segsum_buffers: List of buffers for segment summaries | ||
80 | * @sb_payload_buffers: List of buffers for segment payload | ||
81 | * @sb_io_error: I/O error status | ||
82 | */ | ||
83 | struct nilfs_segment_buffer { | ||
84 | struct super_block *sb_super; | ||
85 | struct list_head sb_list; | ||
86 | struct nilfs_segment_entry *sb_segent; | ||
87 | |||
88 | /* Segment information */ | ||
89 | struct nilfs_segsum_info sb_sum; | ||
90 | __u64 sb_segnum; | ||
91 | __u64 sb_nextnum; | ||
92 | sector_t sb_fseg_start, sb_fseg_end; | ||
93 | sector_t sb_pseg_start; | ||
94 | unsigned sb_rest_blocks; | ||
95 | |||
96 | /* Buffers */ | ||
97 | struct list_head sb_segsum_buffers; | ||
98 | struct list_head sb_payload_buffers; /* including super root */ | ||
99 | |||
100 | /* io status */ | ||
101 | int sb_io_error; | ||
102 | }; | ||
103 | |||
104 | #define NILFS_LIST_SEGBUF(head) \ | ||
105 | list_entry((head), struct nilfs_segment_buffer, sb_list) | ||
106 | #define NILFS_NEXT_SEGBUF(segbuf) NILFS_LIST_SEGBUF((segbuf)->sb_list.next) | ||
107 | #define NILFS_PREV_SEGBUF(segbuf) NILFS_LIST_SEGBUF((segbuf)->sb_list.prev) | ||
108 | #define NILFS_LAST_SEGBUF(head) NILFS_LIST_SEGBUF((head)->prev) | ||
109 | #define NILFS_FIRST_SEGBUF(head) NILFS_LIST_SEGBUF((head)->next) | ||
110 | #define NILFS_SEGBUF_IS_LAST(segbuf, head) ((segbuf)->sb_list.next == (head)) | ||
111 | |||
112 | #define nilfs_for_each_segbuf_before(s, t, h) \ | ||
113 | for ((s) = NILFS_FIRST_SEGBUF(h); (s) != (t); \ | ||
114 | (s) = NILFS_NEXT_SEGBUF(s)) | ||
115 | |||
116 | #define NILFS_SEGBUF_FIRST_BH(head) \ | ||
117 | (list_entry((head)->next, struct buffer_head, b_assoc_buffers)) | ||
118 | #define NILFS_SEGBUF_NEXT_BH(bh) \ | ||
119 | (list_entry((bh)->b_assoc_buffers.next, struct buffer_head, \ | ||
120 | b_assoc_buffers)) | ||
121 | #define NILFS_SEGBUF_BH_IS_LAST(bh, head) ((bh)->b_assoc_buffers.next == head) | ||
122 | |||
123 | |||
124 | int __init nilfs_init_segbuf_cache(void); | ||
125 | void nilfs_destroy_segbuf_cache(void); | ||
126 | struct nilfs_segment_buffer *nilfs_segbuf_new(struct super_block *); | ||
127 | void nilfs_segbuf_free(struct nilfs_segment_buffer *); | ||
128 | int nilfs_segbuf_map(struct nilfs_segment_buffer *, __u64, unsigned long, | ||
129 | struct the_nilfs *); | ||
130 | void nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer *, __u64, | ||
131 | struct the_nilfs *); | ||
132 | int nilfs_segbuf_reset(struct nilfs_segment_buffer *, unsigned, time_t); | ||
133 | int nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer *); | ||
134 | int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer *, | ||
135 | struct buffer_head **); | ||
136 | void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer *); | ||
137 | void nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer *, u32); | ||
138 | void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer *, u32); | ||
139 | |||
140 | static inline void | ||
141 | nilfs_segbuf_add_segsum_buffer(struct nilfs_segment_buffer *segbuf, | ||
142 | struct buffer_head *bh) | ||
143 | { | ||
144 | list_add_tail(&bh->b_assoc_buffers, &segbuf->sb_segsum_buffers); | ||
145 | segbuf->sb_sum.nblocks++; | ||
146 | segbuf->sb_sum.nsumblk++; | ||
147 | } | ||
148 | |||
149 | static inline void | ||
150 | nilfs_segbuf_add_payload_buffer(struct nilfs_segment_buffer *segbuf, | ||
151 | struct buffer_head *bh) | ||
152 | { | ||
153 | list_add_tail(&bh->b_assoc_buffers, &segbuf->sb_payload_buffers); | ||
154 | segbuf->sb_sum.nblocks++; | ||
155 | } | ||
156 | |||
157 | static inline void | ||
158 | nilfs_segbuf_add_file_buffer(struct nilfs_segment_buffer *segbuf, | ||
159 | struct buffer_head *bh) | ||
160 | { | ||
161 | get_bh(bh); | ||
162 | nilfs_segbuf_add_payload_buffer(segbuf, bh); | ||
163 | segbuf->sb_sum.nfileblk++; | ||
164 | } | ||
165 | |||
166 | void nilfs_release_buffers(struct list_head *); | ||
167 | |||
168 | static inline void nilfs_segbuf_clear(struct nilfs_segment_buffer *segbuf) | ||
169 | { | ||
170 | nilfs_release_buffers(&segbuf->sb_segsum_buffers); | ||
171 | nilfs_release_buffers(&segbuf->sb_payload_buffers); | ||
172 | } | ||
173 | |||
174 | struct nilfs_write_info { | ||
175 | struct bio *bio; | ||
176 | int start, end; /* The region to be submitted */ | ||
177 | int rest_blocks; | ||
178 | int max_pages; | ||
179 | int nr_vecs; | ||
180 | sector_t blocknr; | ||
181 | |||
182 | int nbio; | ||
183 | atomic_t err; | ||
184 | struct completion bio_event; | ||
185 | /* completion event of segment write */ | ||
186 | |||
187 | /* | ||
188 | * The following fields must be set explicitly | ||
189 | */ | ||
190 | struct super_block *sb; | ||
191 | struct backing_dev_info *bdi; /* backing dev info */ | ||
192 | struct buffer_head *bh_sr; | ||
193 | }; | ||
194 | |||
195 | |||
196 | void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *, | ||
197 | struct nilfs_write_info *); | ||
198 | int nilfs_segbuf_write(struct nilfs_segment_buffer *, | ||
199 | struct nilfs_write_info *); | ||
200 | int nilfs_segbuf_wait(struct nilfs_segment_buffer *, | ||
201 | struct nilfs_write_info *); | ||
202 | |||
203 | #endif /* _NILFS_SEGBUF_H */ | ||