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