Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 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" |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 29 | |
| 30 | |
| 31 | static struct kmem_cache *nilfs_segbuf_cachep; |
| 32 | |
| 33 | static void nilfs_segbuf_init_once(void *obj) |
| 34 | { |
| 35 | memset(obj, 0, sizeof(struct nilfs_segment_buffer)); |
| 36 | } |
| 37 | |
| 38 | int __init nilfs_init_segbuf_cache(void) |
| 39 | { |
| 40 | nilfs_segbuf_cachep = |
| 41 | kmem_cache_create("nilfs2_segbuf_cache", |
| 42 | sizeof(struct nilfs_segment_buffer), |
| 43 | 0, SLAB_RECLAIM_ACCOUNT, |
| 44 | nilfs_segbuf_init_once); |
| 45 | |
| 46 | return (nilfs_segbuf_cachep == NULL) ? -ENOMEM : 0; |
| 47 | } |
| 48 | |
| 49 | void nilfs_destroy_segbuf_cache(void) |
| 50 | { |
| 51 | kmem_cache_destroy(nilfs_segbuf_cachep); |
| 52 | } |
| 53 | |
| 54 | struct nilfs_segment_buffer *nilfs_segbuf_new(struct super_block *sb) |
| 55 | { |
| 56 | struct nilfs_segment_buffer *segbuf; |
| 57 | |
| 58 | segbuf = kmem_cache_alloc(nilfs_segbuf_cachep, GFP_NOFS); |
| 59 | if (unlikely(!segbuf)) |
| 60 | return NULL; |
| 61 | |
| 62 | segbuf->sb_super = sb; |
| 63 | INIT_LIST_HEAD(&segbuf->sb_list); |
| 64 | INIT_LIST_HEAD(&segbuf->sb_segsum_buffers); |
| 65 | INIT_LIST_HEAD(&segbuf->sb_payload_buffers); |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 66 | |
| 67 | init_completion(&segbuf->sb_bio_event); |
| 68 | atomic_set(&segbuf->sb_err, 0); |
| 69 | segbuf->sb_nbio = 0; |
| 70 | |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 71 | return segbuf; |
| 72 | } |
| 73 | |
| 74 | void nilfs_segbuf_free(struct nilfs_segment_buffer *segbuf) |
| 75 | { |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 76 | kmem_cache_free(nilfs_segbuf_cachep, segbuf); |
| 77 | } |
| 78 | |
Ryusuke Konishi | cece552 | 2009-04-06 19:01:58 -0700 | [diff] [blame] | 79 | void nilfs_segbuf_map(struct nilfs_segment_buffer *segbuf, __u64 segnum, |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 80 | unsigned long offset, struct the_nilfs *nilfs) |
| 81 | { |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 82 | segbuf->sb_segnum = segnum; |
| 83 | nilfs_get_segment_range(nilfs, segnum, &segbuf->sb_fseg_start, |
| 84 | &segbuf->sb_fseg_end); |
| 85 | |
| 86 | segbuf->sb_pseg_start = segbuf->sb_fseg_start + offset; |
| 87 | segbuf->sb_rest_blocks = |
| 88 | segbuf->sb_fseg_end - segbuf->sb_pseg_start + 1; |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer *segbuf, |
| 92 | __u64 nextnum, struct the_nilfs *nilfs) |
| 93 | { |
| 94 | segbuf->sb_nextnum = nextnum; |
| 95 | segbuf->sb_sum.next = nilfs_get_segment_start_blocknr(nilfs, nextnum); |
| 96 | } |
| 97 | |
| 98 | int nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer *segbuf) |
| 99 | { |
| 100 | struct buffer_head *bh; |
| 101 | |
| 102 | bh = sb_getblk(segbuf->sb_super, |
| 103 | segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk); |
| 104 | if (unlikely(!bh)) |
| 105 | return -ENOMEM; |
| 106 | |
| 107 | nilfs_segbuf_add_segsum_buffer(segbuf, bh); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer *segbuf, |
| 112 | struct buffer_head **bhp) |
| 113 | { |
| 114 | struct buffer_head *bh; |
| 115 | |
| 116 | bh = sb_getblk(segbuf->sb_super, |
| 117 | segbuf->sb_pseg_start + segbuf->sb_sum.nblocks); |
| 118 | if (unlikely(!bh)) |
| 119 | return -ENOMEM; |
| 120 | |
| 121 | nilfs_segbuf_add_payload_buffer(segbuf, bh); |
| 122 | *bhp = bh; |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int nilfs_segbuf_reset(struct nilfs_segment_buffer *segbuf, unsigned flags, |
| 127 | time_t ctime) |
| 128 | { |
| 129 | int err; |
| 130 | |
| 131 | segbuf->sb_sum.nblocks = segbuf->sb_sum.nsumblk = 0; |
| 132 | err = nilfs_segbuf_extend_segsum(segbuf); |
| 133 | if (unlikely(err)) |
| 134 | return err; |
| 135 | |
| 136 | segbuf->sb_sum.flags = flags; |
| 137 | segbuf->sb_sum.sumbytes = sizeof(struct nilfs_segment_summary); |
| 138 | segbuf->sb_sum.nfinfo = segbuf->sb_sum.nfileblk = 0; |
| 139 | segbuf->sb_sum.ctime = ctime; |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Setup segument summary |
| 145 | */ |
| 146 | void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer *segbuf) |
| 147 | { |
| 148 | struct nilfs_segment_summary *raw_sum; |
| 149 | struct buffer_head *bh_sum; |
| 150 | |
| 151 | bh_sum = list_entry(segbuf->sb_segsum_buffers.next, |
| 152 | struct buffer_head, b_assoc_buffers); |
| 153 | raw_sum = (struct nilfs_segment_summary *)bh_sum->b_data; |
| 154 | |
| 155 | raw_sum->ss_magic = cpu_to_le32(NILFS_SEGSUM_MAGIC); |
| 156 | raw_sum->ss_bytes = cpu_to_le16(sizeof(*raw_sum)); |
| 157 | raw_sum->ss_flags = cpu_to_le16(segbuf->sb_sum.flags); |
| 158 | raw_sum->ss_seq = cpu_to_le64(segbuf->sb_sum.seg_seq); |
| 159 | raw_sum->ss_create = cpu_to_le64(segbuf->sb_sum.ctime); |
| 160 | raw_sum->ss_next = cpu_to_le64(segbuf->sb_sum.next); |
| 161 | raw_sum->ss_nblocks = cpu_to_le32(segbuf->sb_sum.nblocks); |
| 162 | raw_sum->ss_nfinfo = cpu_to_le32(segbuf->sb_sum.nfinfo); |
| 163 | raw_sum->ss_sumbytes = cpu_to_le32(segbuf->sb_sum.sumbytes); |
| 164 | raw_sum->ss_pad = 0; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * CRC calculation routines |
| 169 | */ |
| 170 | void nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer *segbuf, |
| 171 | u32 seed) |
| 172 | { |
| 173 | struct buffer_head *bh; |
| 174 | struct nilfs_segment_summary *raw_sum; |
| 175 | unsigned long size, bytes = segbuf->sb_sum.sumbytes; |
| 176 | u32 crc; |
| 177 | |
| 178 | bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head, |
| 179 | b_assoc_buffers); |
| 180 | |
| 181 | raw_sum = (struct nilfs_segment_summary *)bh->b_data; |
| 182 | size = min_t(unsigned long, bytes, bh->b_size); |
| 183 | crc = crc32_le(seed, |
| 184 | (unsigned char *)raw_sum + |
| 185 | sizeof(raw_sum->ss_datasum) + sizeof(raw_sum->ss_sumsum), |
| 186 | size - (sizeof(raw_sum->ss_datasum) + |
| 187 | sizeof(raw_sum->ss_sumsum))); |
| 188 | |
| 189 | list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers, |
| 190 | b_assoc_buffers) { |
| 191 | bytes -= size; |
| 192 | size = min_t(unsigned long, bytes, bh->b_size); |
| 193 | crc = crc32_le(crc, bh->b_data, size); |
| 194 | } |
| 195 | raw_sum->ss_sumsum = cpu_to_le32(crc); |
| 196 | } |
| 197 | |
| 198 | void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer *segbuf, |
| 199 | u32 seed) |
| 200 | { |
| 201 | struct buffer_head *bh; |
| 202 | struct nilfs_segment_summary *raw_sum; |
| 203 | void *kaddr; |
| 204 | u32 crc; |
| 205 | |
| 206 | bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head, |
| 207 | b_assoc_buffers); |
| 208 | raw_sum = (struct nilfs_segment_summary *)bh->b_data; |
| 209 | crc = crc32_le(seed, |
| 210 | (unsigned char *)raw_sum + sizeof(raw_sum->ss_datasum), |
| 211 | bh->b_size - sizeof(raw_sum->ss_datasum)); |
| 212 | |
| 213 | list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers, |
| 214 | b_assoc_buffers) { |
| 215 | crc = crc32_le(crc, bh->b_data, bh->b_size); |
| 216 | } |
| 217 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) { |
| 218 | kaddr = kmap_atomic(bh->b_page, KM_USER0); |
| 219 | crc = crc32_le(crc, kaddr + bh_offset(bh), bh->b_size); |
| 220 | kunmap_atomic(kaddr, KM_USER0); |
| 221 | } |
| 222 | raw_sum->ss_datasum = cpu_to_le32(crc); |
| 223 | } |
| 224 | |
| 225 | void nilfs_release_buffers(struct list_head *list) |
| 226 | { |
| 227 | struct buffer_head *bh, *n; |
| 228 | |
| 229 | list_for_each_entry_safe(bh, n, list, b_assoc_buffers) { |
| 230 | list_del_init(&bh->b_assoc_buffers); |
| 231 | if (buffer_nilfs_allocated(bh)) { |
| 232 | struct page *clone_page = bh->b_page; |
| 233 | |
| 234 | /* remove clone page */ |
| 235 | brelse(bh); |
| 236 | page_cache_release(clone_page); /* for each bh */ |
| 237 | if (page_count(clone_page) <= 2) { |
| 238 | lock_page(clone_page); |
| 239 | nilfs_free_private_page(clone_page); |
| 240 | } |
| 241 | continue; |
| 242 | } |
| 243 | brelse(bh); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * BIO operations |
| 249 | */ |
| 250 | static void nilfs_end_bio_write(struct bio *bio, int err) |
| 251 | { |
| 252 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 253 | struct nilfs_segment_buffer *segbuf = bio->bi_private; |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 254 | |
| 255 | if (err == -EOPNOTSUPP) { |
| 256 | set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); |
| 257 | bio_put(bio); |
| 258 | /* to be detected by submit_seg_bio() */ |
| 259 | } |
| 260 | |
| 261 | if (!uptodate) |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 262 | atomic_inc(&segbuf->sb_err); |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 263 | |
| 264 | bio_put(bio); |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 265 | complete(&segbuf->sb_bio_event); |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 268 | static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer *segbuf, |
| 269 | struct nilfs_write_info *wi, int mode) |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 270 | { |
| 271 | struct bio *bio = wi->bio; |
| 272 | int err; |
| 273 | |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 274 | if (segbuf->sb_nbio > 0 && bdi_write_congested(wi->bdi)) { |
| 275 | wait_for_completion(&segbuf->sb_bio_event); |
| 276 | segbuf->sb_nbio--; |
| 277 | if (unlikely(atomic_read(&segbuf->sb_err))) { |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 278 | bio_put(bio); |
| 279 | err = -EIO; |
| 280 | goto failed; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | bio->bi_end_io = nilfs_end_bio_write; |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 285 | bio->bi_private = segbuf; |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 286 | bio_get(bio); |
| 287 | submit_bio(mode, bio); |
| 288 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) { |
| 289 | bio_put(bio); |
| 290 | err = -EOPNOTSUPP; |
| 291 | goto failed; |
| 292 | } |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 293 | segbuf->sb_nbio++; |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 294 | bio_put(bio); |
| 295 | |
| 296 | wi->bio = NULL; |
| 297 | wi->rest_blocks -= wi->end - wi->start; |
| 298 | wi->nr_vecs = min(wi->max_pages, wi->rest_blocks); |
| 299 | wi->start = wi->end; |
| 300 | return 0; |
| 301 | |
| 302 | failed: |
| 303 | wi->bio = NULL; |
| 304 | return err; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * nilfs_alloc_seg_bio - allocate a bio for writing segment. |
| 309 | * @sb: super block |
| 310 | * @start: beginning disk block number of this BIO. |
| 311 | * @nr_vecs: request size of page vector. |
| 312 | * |
| 313 | * alloc_seg_bio() allocates a new BIO structure and initialize it. |
| 314 | * |
| 315 | * Return Value: On success, pointer to the struct bio is returned. |
| 316 | * On error, NULL is returned. |
| 317 | */ |
| 318 | static struct bio *nilfs_alloc_seg_bio(struct super_block *sb, sector_t start, |
| 319 | int nr_vecs) |
| 320 | { |
| 321 | struct bio *bio; |
| 322 | |
Ryusuke Konishi | c1b353f | 2009-06-19 15:25:42 +0900 | [diff] [blame] | 323 | bio = bio_alloc(GFP_NOIO, nr_vecs); |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 324 | if (bio == NULL) { |
| 325 | while (!bio && (nr_vecs >>= 1)) |
Ryusuke Konishi | c1b353f | 2009-06-19 15:25:42 +0900 | [diff] [blame] | 326 | bio = bio_alloc(GFP_NOIO, nr_vecs); |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 327 | } |
| 328 | if (likely(bio)) { |
| 329 | bio->bi_bdev = sb->s_bdev; |
| 330 | bio->bi_sector = (sector_t)start << (sb->s_blocksize_bits - 9); |
| 331 | } |
| 332 | return bio; |
| 333 | } |
| 334 | |
| 335 | void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf, |
| 336 | struct nilfs_write_info *wi) |
| 337 | { |
| 338 | wi->bio = NULL; |
| 339 | wi->rest_blocks = segbuf->sb_sum.nblocks; |
| 340 | wi->max_pages = bio_get_nr_vecs(wi->sb->s_bdev); |
| 341 | wi->nr_vecs = min(wi->max_pages, wi->rest_blocks); |
| 342 | wi->start = wi->end = 0; |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 343 | wi->blocknr = segbuf->sb_pseg_start; |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 346 | static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer *segbuf, |
| 347 | struct nilfs_write_info *wi, |
| 348 | struct buffer_head *bh, int mode) |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 349 | { |
| 350 | int len, err; |
| 351 | |
| 352 | BUG_ON(wi->nr_vecs <= 0); |
| 353 | repeat: |
| 354 | if (!wi->bio) { |
| 355 | wi->bio = nilfs_alloc_seg_bio(wi->sb, wi->blocknr + wi->end, |
| 356 | wi->nr_vecs); |
| 357 | if (unlikely(!wi->bio)) |
| 358 | return -ENOMEM; |
| 359 | } |
| 360 | |
| 361 | len = bio_add_page(wi->bio, bh->b_page, bh->b_size, bh_offset(bh)); |
| 362 | if (len == bh->b_size) { |
| 363 | wi->end++; |
| 364 | return 0; |
| 365 | } |
| 366 | /* bio is FULL */ |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 367 | err = nilfs_segbuf_submit_bio(segbuf, wi, mode); |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 368 | /* never submit current bh */ |
| 369 | if (likely(!err)) |
| 370 | goto repeat; |
| 371 | return err; |
| 372 | } |
| 373 | |
| 374 | int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf, |
| 375 | struct nilfs_write_info *wi) |
| 376 | { |
| 377 | struct buffer_head *bh; |
Ryusuke Konishi | 5f1586d | 2009-11-29 19:14:17 +0900 | [diff] [blame] | 378 | int res = 0, rw = WRITE; |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 379 | |
| 380 | list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) { |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 381 | res = nilfs_segbuf_submit_bh(segbuf, wi, bh, rw); |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 382 | if (unlikely(res)) |
| 383 | goto failed_bio; |
| 384 | } |
| 385 | |
| 386 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) { |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 387 | res = nilfs_segbuf_submit_bh(segbuf, wi, bh, rw); |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 388 | if (unlikely(res)) |
| 389 | goto failed_bio; |
| 390 | } |
| 391 | |
| 392 | if (wi->bio) { |
| 393 | /* |
| 394 | * Last BIO is always sent through the following |
| 395 | * submission. |
| 396 | */ |
Ryusuke Konishi | 30bda0b | 2009-05-17 10:57:43 +0900 | [diff] [blame] | 397 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 398 | res = nilfs_segbuf_submit_bio(segbuf, wi, rw); |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 401 | failed_bio: |
Ryusuke Konishi | 5f1586d | 2009-11-29 19:14:17 +0900 | [diff] [blame] | 402 | return res; |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
| 406 | * nilfs_segbuf_wait - wait for completion of requested BIOs |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 407 | * @segbuf: segment buffer |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 408 | * |
| 409 | * Return Value: On Success, 0 is returned. On Error, one of the following |
| 410 | * negative error code is returned. |
| 411 | * |
| 412 | * %-EIO - I/O error |
| 413 | */ |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 414 | int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf) |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 415 | { |
| 416 | int err = 0; |
| 417 | |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 418 | if (!segbuf->sb_nbio) |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 419 | return 0; |
| 420 | |
| 421 | do { |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 422 | wait_for_completion(&segbuf->sb_bio_event); |
| 423 | } while (--segbuf->sb_nbio > 0); |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 424 | |
Ryusuke Konishi | 9284ad2 | 2009-11-25 01:04:21 +0900 | [diff] [blame^] | 425 | if (unlikely(atomic_read(&segbuf->sb_err) > 0)) { |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 426 | printk(KERN_ERR "NILFS: IO error writing segment\n"); |
| 427 | err = -EIO; |
Ryusuke Konishi | 64b5a32 | 2009-04-06 19:01:36 -0700 | [diff] [blame] | 428 | } |
| 429 | return err; |
| 430 | } |