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