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