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