2 * segbuf.c - NILFS segment buffer
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
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
20 * Written by Ryusuke Konishi <ryusuke@osrg.net>
24 #include <linux/buffer_head.h>
25 #include <linux/writeback.h>
26 #include <linux/crc32.h>
27 #include <linux/backing-dev.h>
28 #include <linux/slab.h>
33 struct nilfs_write_info
{
34 struct the_nilfs
*nilfs
;
36 int start
, end
; /* The region to be submitted */
44 static int nilfs_segbuf_write(struct nilfs_segment_buffer
*segbuf
,
45 struct the_nilfs
*nilfs
);
46 static int nilfs_segbuf_wait(struct nilfs_segment_buffer
*segbuf
);
49 static struct kmem_cache
*nilfs_segbuf_cachep
;
51 static void nilfs_segbuf_init_once(void *obj
)
53 memset(obj
, 0, sizeof(struct nilfs_segment_buffer
));
56 int __init
nilfs_init_segbuf_cache(void)
59 kmem_cache_create("nilfs2_segbuf_cache",
60 sizeof(struct nilfs_segment_buffer
),
61 0, SLAB_RECLAIM_ACCOUNT
,
62 nilfs_segbuf_init_once
);
64 return (nilfs_segbuf_cachep
== NULL
) ? -ENOMEM
: 0;
67 void nilfs_destroy_segbuf_cache(void)
69 kmem_cache_destroy(nilfs_segbuf_cachep
);
72 struct nilfs_segment_buffer
*nilfs_segbuf_new(struct super_block
*sb
)
74 struct nilfs_segment_buffer
*segbuf
;
76 segbuf
= kmem_cache_alloc(nilfs_segbuf_cachep
, GFP_NOFS
);
77 if (unlikely(!segbuf
))
80 segbuf
->sb_super
= sb
;
81 INIT_LIST_HEAD(&segbuf
->sb_list
);
82 INIT_LIST_HEAD(&segbuf
->sb_segsum_buffers
);
83 INIT_LIST_HEAD(&segbuf
->sb_payload_buffers
);
85 init_completion(&segbuf
->sb_bio_event
);
86 atomic_set(&segbuf
->sb_err
, 0);
92 void nilfs_segbuf_free(struct nilfs_segment_buffer
*segbuf
)
94 kmem_cache_free(nilfs_segbuf_cachep
, segbuf
);
97 void nilfs_segbuf_map(struct nilfs_segment_buffer
*segbuf
, __u64 segnum
,
98 unsigned long offset
, struct the_nilfs
*nilfs
)
100 segbuf
->sb_segnum
= segnum
;
101 nilfs_get_segment_range(nilfs
, segnum
, &segbuf
->sb_fseg_start
,
102 &segbuf
->sb_fseg_end
);
104 segbuf
->sb_pseg_start
= segbuf
->sb_fseg_start
+ offset
;
105 segbuf
->sb_rest_blocks
=
106 segbuf
->sb_fseg_end
- segbuf
->sb_pseg_start
+ 1;
110 * nilfs_segbuf_map_cont - map a new log behind a given log
111 * @segbuf: new segment buffer
112 * @prev: segment buffer containing a log to be continued
114 void nilfs_segbuf_map_cont(struct nilfs_segment_buffer
*segbuf
,
115 struct nilfs_segment_buffer
*prev
)
117 segbuf
->sb_segnum
= prev
->sb_segnum
;
118 segbuf
->sb_fseg_start
= prev
->sb_fseg_start
;
119 segbuf
->sb_fseg_end
= prev
->sb_fseg_end
;
120 segbuf
->sb_pseg_start
= prev
->sb_pseg_start
+ prev
->sb_sum
.nblocks
;
121 segbuf
->sb_rest_blocks
=
122 segbuf
->sb_fseg_end
- segbuf
->sb_pseg_start
+ 1;
125 void nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer
*segbuf
,
126 __u64 nextnum
, struct the_nilfs
*nilfs
)
128 segbuf
->sb_nextnum
= nextnum
;
129 segbuf
->sb_sum
.next
= nilfs_get_segment_start_blocknr(nilfs
, nextnum
);
132 int nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer
*segbuf
)
134 struct buffer_head
*bh
;
136 bh
= sb_getblk(segbuf
->sb_super
,
137 segbuf
->sb_pseg_start
+ segbuf
->sb_sum
.nsumblk
);
141 nilfs_segbuf_add_segsum_buffer(segbuf
, bh
);
145 int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer
*segbuf
,
146 struct buffer_head
**bhp
)
148 struct buffer_head
*bh
;
150 bh
= sb_getblk(segbuf
->sb_super
,
151 segbuf
->sb_pseg_start
+ segbuf
->sb_sum
.nblocks
);
155 nilfs_segbuf_add_payload_buffer(segbuf
, bh
);
160 int nilfs_segbuf_reset(struct nilfs_segment_buffer
*segbuf
, unsigned flags
,
165 segbuf
->sb_sum
.nblocks
= segbuf
->sb_sum
.nsumblk
= 0;
166 err
= nilfs_segbuf_extend_segsum(segbuf
);
170 segbuf
->sb_sum
.flags
= flags
;
171 segbuf
->sb_sum
.sumbytes
= sizeof(struct nilfs_segment_summary
);
172 segbuf
->sb_sum
.nfinfo
= segbuf
->sb_sum
.nfileblk
= 0;
173 segbuf
->sb_sum
.ctime
= ctime
;
178 * Setup segment summary
180 void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer
*segbuf
)
182 struct nilfs_segment_summary
*raw_sum
;
183 struct buffer_head
*bh_sum
;
185 bh_sum
= list_entry(segbuf
->sb_segsum_buffers
.next
,
186 struct buffer_head
, b_assoc_buffers
);
187 raw_sum
= (struct nilfs_segment_summary
*)bh_sum
->b_data
;
189 raw_sum
->ss_magic
= cpu_to_le32(NILFS_SEGSUM_MAGIC
);
190 raw_sum
->ss_bytes
= cpu_to_le16(sizeof(*raw_sum
));
191 raw_sum
->ss_flags
= cpu_to_le16(segbuf
->sb_sum
.flags
);
192 raw_sum
->ss_seq
= cpu_to_le64(segbuf
->sb_sum
.seg_seq
);
193 raw_sum
->ss_create
= cpu_to_le64(segbuf
->sb_sum
.ctime
);
194 raw_sum
->ss_next
= cpu_to_le64(segbuf
->sb_sum
.next
);
195 raw_sum
->ss_nblocks
= cpu_to_le32(segbuf
->sb_sum
.nblocks
);
196 raw_sum
->ss_nfinfo
= cpu_to_le32(segbuf
->sb_sum
.nfinfo
);
197 raw_sum
->ss_sumbytes
= cpu_to_le32(segbuf
->sb_sum
.sumbytes
);
202 * CRC calculation routines
204 void nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer
*segbuf
,
207 struct buffer_head
*bh
;
208 struct nilfs_segment_summary
*raw_sum
;
209 unsigned long size
, bytes
= segbuf
->sb_sum
.sumbytes
;
212 bh
= list_entry(segbuf
->sb_segsum_buffers
.next
, struct buffer_head
,
215 raw_sum
= (struct nilfs_segment_summary
*)bh
->b_data
;
216 size
= min_t(unsigned long, bytes
, bh
->b_size
);
218 (unsigned char *)raw_sum
+
219 sizeof(raw_sum
->ss_datasum
) + sizeof(raw_sum
->ss_sumsum
),
220 size
- (sizeof(raw_sum
->ss_datasum
) +
221 sizeof(raw_sum
->ss_sumsum
)));
223 list_for_each_entry_continue(bh
, &segbuf
->sb_segsum_buffers
,
226 size
= min_t(unsigned long, bytes
, bh
->b_size
);
227 crc
= crc32_le(crc
, bh
->b_data
, size
);
229 raw_sum
->ss_sumsum
= cpu_to_le32(crc
);
232 void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer
*segbuf
,
235 struct buffer_head
*bh
;
236 struct nilfs_segment_summary
*raw_sum
;
240 bh
= list_entry(segbuf
->sb_segsum_buffers
.next
, struct buffer_head
,
242 raw_sum
= (struct nilfs_segment_summary
*)bh
->b_data
;
244 (unsigned char *)raw_sum
+ sizeof(raw_sum
->ss_datasum
),
245 bh
->b_size
- sizeof(raw_sum
->ss_datasum
));
247 list_for_each_entry_continue(bh
, &segbuf
->sb_segsum_buffers
,
249 crc
= crc32_le(crc
, bh
->b_data
, bh
->b_size
);
251 list_for_each_entry(bh
, &segbuf
->sb_payload_buffers
, b_assoc_buffers
) {
252 kaddr
= kmap_atomic(bh
->b_page
, KM_USER0
);
253 crc
= crc32_le(crc
, kaddr
+ bh_offset(bh
), bh
->b_size
);
254 kunmap_atomic(kaddr
, KM_USER0
);
256 raw_sum
->ss_datasum
= cpu_to_le32(crc
);
259 static void nilfs_release_buffers(struct list_head
*list
)
261 struct buffer_head
*bh
, *n
;
263 list_for_each_entry_safe(bh
, n
, list
, b_assoc_buffers
) {
264 list_del_init(&bh
->b_assoc_buffers
);
265 if (buffer_nilfs_allocated(bh
)) {
266 struct page
*clone_page
= bh
->b_page
;
268 /* remove clone page */
270 page_cache_release(clone_page
); /* for each bh */
271 if (page_count(clone_page
) <= 2) {
272 lock_page(clone_page
);
273 nilfs_free_private_page(clone_page
);
281 static void nilfs_segbuf_clear(struct nilfs_segment_buffer
*segbuf
)
283 nilfs_release_buffers(&segbuf
->sb_segsum_buffers
);
284 nilfs_release_buffers(&segbuf
->sb_payload_buffers
);
288 * Iterators for segment buffers
290 void nilfs_clear_logs(struct list_head
*logs
)
292 struct nilfs_segment_buffer
*segbuf
;
294 list_for_each_entry(segbuf
, logs
, sb_list
)
295 nilfs_segbuf_clear(segbuf
);
298 void nilfs_truncate_logs(struct list_head
*logs
,
299 struct nilfs_segment_buffer
*last
)
301 struct nilfs_segment_buffer
*n
, *segbuf
;
303 segbuf
= list_prepare_entry(last
, logs
, sb_list
);
304 list_for_each_entry_safe_continue(segbuf
, n
, logs
, sb_list
) {
305 list_del_init(&segbuf
->sb_list
);
306 nilfs_segbuf_clear(segbuf
);
307 nilfs_segbuf_free(segbuf
);
311 int nilfs_write_logs(struct list_head
*logs
, struct the_nilfs
*nilfs
)
313 struct nilfs_segment_buffer
*segbuf
;
316 list_for_each_entry(segbuf
, logs
, sb_list
) {
317 ret
= nilfs_segbuf_write(segbuf
, nilfs
);
324 int nilfs_wait_on_logs(struct list_head
*logs
)
326 struct nilfs_segment_buffer
*segbuf
;
329 list_for_each_entry(segbuf
, logs
, sb_list
) {
330 err
= nilfs_segbuf_wait(segbuf
);
340 static void nilfs_end_bio_write(struct bio
*bio
, int err
)
342 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
343 struct nilfs_segment_buffer
*segbuf
= bio
->bi_private
;
345 if (err
== -EOPNOTSUPP
) {
346 set_bit(BIO_EOPNOTSUPP
, &bio
->bi_flags
);
348 /* to be detected by submit_seg_bio() */
352 atomic_inc(&segbuf
->sb_err
);
355 complete(&segbuf
->sb_bio_event
);
358 static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer
*segbuf
,
359 struct nilfs_write_info
*wi
, int mode
)
361 struct bio
*bio
= wi
->bio
;
364 if (segbuf
->sb_nbio
> 0 && bdi_write_congested(wi
->nilfs
->ns_bdi
)) {
365 wait_for_completion(&segbuf
->sb_bio_event
);
367 if (unlikely(atomic_read(&segbuf
->sb_err
))) {
374 bio
->bi_end_io
= nilfs_end_bio_write
;
375 bio
->bi_private
= segbuf
;
377 submit_bio(mode
, bio
);
378 if (bio_flagged(bio
, BIO_EOPNOTSUPP
)) {
387 wi
->rest_blocks
-= wi
->end
- wi
->start
;
388 wi
->nr_vecs
= min(wi
->max_pages
, wi
->rest_blocks
);
398 * nilfs_alloc_seg_bio - allocate a new bio for writing log
399 * @nilfs: nilfs object
400 * @start: start block number of the bio
401 * @nr_vecs: request size of page vector.
403 * Return Value: On success, pointer to the struct bio is returned.
404 * On error, NULL is returned.
406 static struct bio
*nilfs_alloc_seg_bio(struct the_nilfs
*nilfs
, sector_t start
,
411 bio
= bio_alloc(GFP_NOIO
, nr_vecs
);
413 while (!bio
&& (nr_vecs
>>= 1))
414 bio
= bio_alloc(GFP_NOIO
, nr_vecs
);
417 bio
->bi_bdev
= nilfs
->ns_bdev
;
418 bio
->bi_sector
= start
<< (nilfs
->ns_blocksize_bits
- 9);
423 static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer
*segbuf
,
424 struct nilfs_write_info
*wi
)
427 wi
->rest_blocks
= segbuf
->sb_sum
.nblocks
;
428 wi
->max_pages
= bio_get_nr_vecs(wi
->nilfs
->ns_bdev
);
429 wi
->nr_vecs
= min(wi
->max_pages
, wi
->rest_blocks
);
430 wi
->start
= wi
->end
= 0;
431 wi
->blocknr
= segbuf
->sb_pseg_start
;
434 static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer
*segbuf
,
435 struct nilfs_write_info
*wi
,
436 struct buffer_head
*bh
, int mode
)
440 BUG_ON(wi
->nr_vecs
<= 0);
443 wi
->bio
= nilfs_alloc_seg_bio(wi
->nilfs
, wi
->blocknr
+ wi
->end
,
445 if (unlikely(!wi
->bio
))
449 len
= bio_add_page(wi
->bio
, bh
->b_page
, bh
->b_size
, bh_offset(bh
));
450 if (len
== bh
->b_size
) {
455 err
= nilfs_segbuf_submit_bio(segbuf
, wi
, mode
);
456 /* never submit current bh */
463 * nilfs_segbuf_write - submit write requests of a log
464 * @segbuf: buffer storing a log to be written
465 * @nilfs: nilfs object
467 * Return Value: On Success, 0 is returned. On Error, one of the following
468 * negative error code is returned.
472 * %-ENOMEM - Insufficient memory available.
474 static int nilfs_segbuf_write(struct nilfs_segment_buffer
*segbuf
,
475 struct the_nilfs
*nilfs
)
477 struct nilfs_write_info wi
;
478 struct buffer_head
*bh
;
479 int res
= 0, rw
= WRITE
;
482 nilfs_segbuf_prepare_write(segbuf
, &wi
);
484 list_for_each_entry(bh
, &segbuf
->sb_segsum_buffers
, b_assoc_buffers
) {
485 res
= nilfs_segbuf_submit_bh(segbuf
, &wi
, bh
, rw
);
490 list_for_each_entry(bh
, &segbuf
->sb_payload_buffers
, b_assoc_buffers
) {
491 res
= nilfs_segbuf_submit_bh(segbuf
, &wi
, bh
, rw
);
498 * Last BIO is always sent through the following
501 rw
|= (1 << BIO_RW_SYNCIO
) | (1 << BIO_RW_UNPLUG
);
502 res
= nilfs_segbuf_submit_bio(segbuf
, &wi
, rw
);
510 * nilfs_segbuf_wait - wait for completion of requested BIOs
511 * @segbuf: segment buffer
513 * Return Value: On Success, 0 is returned. On Error, one of the following
514 * negative error code is returned.
518 static int nilfs_segbuf_wait(struct nilfs_segment_buffer
*segbuf
)
522 if (!segbuf
->sb_nbio
)
526 wait_for_completion(&segbuf
->sb_bio_event
);
527 } while (--segbuf
->sb_nbio
> 0);
529 if (unlikely(atomic_read(&segbuf
->sb_err
) > 0)) {
530 printk(KERN_ERR
"NILFS: IO error writing segment\n");