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