1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * segbuf.c - 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 20 struct 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 30 static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf, 31 struct the_nilfs *nilfs); 32 static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf); 33 34 struct 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 55 void nilfs_segbuf_free(struct nilfs_segment_buffer *segbuf) 56 { 57 kmem_cache_free(nilfs_segbuf_cachep, segbuf); 58 } 59 60 void 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 */ 77 void 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 88 void 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 95 int 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 108 int 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 123 int 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 */ 144 void 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 */ 169 static void 170 nilfs_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 197 static 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 224 static void 225 nilfs_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 241 static 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 251 static 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 */ 261 void 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 269 void 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 282 int 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 295 int 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 */ 313 void 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 */ 328 static 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 339 static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer *segbuf, 340 struct nilfs_write_info *wi, int mode, 341 int mode_flags) 342 { 343 struct bio *bio = wi->bio; 344 int err; 345 346 if (segbuf->sb_nbio > 0 && 347 bdi_write_congested(segbuf->sb_super->s_bdi)) { 348 wait_for_completion(&segbuf->sb_bio_event); 349 segbuf->sb_nbio--; 350 if (unlikely(atomic_read(&segbuf->sb_err))) { 351 bio_put(bio); 352 err = -EIO; 353 goto failed; 354 } 355 } 356 357 bio->bi_end_io = nilfs_end_bio_write; 358 bio->bi_private = segbuf; 359 bio_set_op_attrs(bio, mode, mode_flags); 360 submit_bio(bio); 361 segbuf->sb_nbio++; 362 363 wi->bio = NULL; 364 wi->rest_blocks -= wi->end - wi->start; 365 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks); 366 wi->start = wi->end; 367 return 0; 368 369 failed: 370 wi->bio = NULL; 371 return err; 372 } 373 374 /** 375 * nilfs_alloc_seg_bio - allocate a new bio for writing log 376 * @nilfs: nilfs object 377 * @start: start block number of the bio 378 * @nr_vecs: request size of page vector. 379 * 380 * Return Value: On success, pointer to the struct bio is returned. 381 * On error, NULL is returned. 382 */ 383 static struct bio *nilfs_alloc_seg_bio(struct the_nilfs *nilfs, sector_t start, 384 int nr_vecs) 385 { 386 struct bio *bio; 387 388 bio = bio_alloc(GFP_NOIO, nr_vecs); 389 if (likely(bio)) { 390 bio_set_dev(bio, nilfs->ns_bdev); 391 bio->bi_iter.bi_sector = 392 start << (nilfs->ns_blocksize_bits - 9); 393 } 394 return bio; 395 } 396 397 static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf, 398 struct nilfs_write_info *wi) 399 { 400 wi->bio = NULL; 401 wi->rest_blocks = segbuf->sb_sum.nblocks; 402 wi->max_pages = BIO_MAX_VECS; 403 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks); 404 wi->start = wi->end = 0; 405 wi->blocknr = segbuf->sb_pseg_start; 406 } 407 408 static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer *segbuf, 409 struct nilfs_write_info *wi, 410 struct buffer_head *bh, int mode) 411 { 412 int len, err; 413 414 BUG_ON(wi->nr_vecs <= 0); 415 repeat: 416 if (!wi->bio) { 417 wi->bio = nilfs_alloc_seg_bio(wi->nilfs, wi->blocknr + wi->end, 418 wi->nr_vecs); 419 if (unlikely(!wi->bio)) 420 return -ENOMEM; 421 } 422 423 len = bio_add_page(wi->bio, bh->b_page, bh->b_size, bh_offset(bh)); 424 if (len == bh->b_size) { 425 wi->end++; 426 return 0; 427 } 428 /* bio is FULL */ 429 err = nilfs_segbuf_submit_bio(segbuf, wi, mode, 0); 430 /* never submit current bh */ 431 if (likely(!err)) 432 goto repeat; 433 return err; 434 } 435 436 /** 437 * nilfs_segbuf_write - submit write requests of a log 438 * @segbuf: buffer storing a log to be written 439 * @nilfs: nilfs object 440 * 441 * Return Value: On Success, 0 is returned. On Error, one of the following 442 * negative error code is returned. 443 * 444 * %-EIO - I/O error 445 * 446 * %-ENOMEM - Insufficient memory available. 447 */ 448 static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf, 449 struct the_nilfs *nilfs) 450 { 451 struct nilfs_write_info wi; 452 struct buffer_head *bh; 453 int res = 0; 454 455 wi.nilfs = nilfs; 456 nilfs_segbuf_prepare_write(segbuf, &wi); 457 458 list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) { 459 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, REQ_OP_WRITE); 460 if (unlikely(res)) 461 goto failed_bio; 462 } 463 464 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) { 465 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, REQ_OP_WRITE); 466 if (unlikely(res)) 467 goto failed_bio; 468 } 469 470 if (wi.bio) { 471 /* 472 * Last BIO is always sent through the following 473 * submission. 474 */ 475 res = nilfs_segbuf_submit_bio(segbuf, &wi, REQ_OP_WRITE, 476 REQ_SYNC); 477 } 478 479 failed_bio: 480 return res; 481 } 482 483 /** 484 * nilfs_segbuf_wait - wait for completion of requested BIOs 485 * @segbuf: segment buffer 486 * 487 * Return Value: On Success, 0 is returned. On Error, one of the following 488 * negative error code is returned. 489 * 490 * %-EIO - I/O error 491 */ 492 static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf) 493 { 494 int err = 0; 495 496 if (!segbuf->sb_nbio) 497 return 0; 498 499 do { 500 wait_for_completion(&segbuf->sb_bio_event); 501 } while (--segbuf->sb_nbio > 0); 502 503 if (unlikely(atomic_read(&segbuf->sb_err) > 0)) { 504 nilfs_err(segbuf->sb_super, 505 "I/O error writing log (start-blocknr=%llu, block-count=%lu) in segment %llu", 506 (unsigned long long)segbuf->sb_pseg_start, 507 segbuf->sb_sum.nblocks, 508 (unsigned long long)segbuf->sb_segnum); 509 err = -EIO; 510 } 511 return err; 512 } 513