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 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 lock_buffer(bh); 105 if (!buffer_uptodate(bh)) { 106 memset(bh->b_data, 0, bh->b_size); 107 set_buffer_uptodate(bh); 108 } 109 unlock_buffer(bh); 110 nilfs_segbuf_add_segsum_buffer(segbuf, bh); 111 return 0; 112 } 113 114 int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer *segbuf, 115 struct buffer_head **bhp) 116 { 117 struct buffer_head *bh; 118 119 bh = sb_getblk(segbuf->sb_super, 120 segbuf->sb_pseg_start + segbuf->sb_sum.nblocks); 121 if (unlikely(!bh)) 122 return -ENOMEM; 123 124 nilfs_segbuf_add_payload_buffer(segbuf, bh); 125 *bhp = bh; 126 return 0; 127 } 128 129 int nilfs_segbuf_reset(struct nilfs_segment_buffer *segbuf, unsigned int flags, 130 time64_t ctime, __u64 cno) 131 { 132 int err; 133 134 segbuf->sb_sum.nblocks = segbuf->sb_sum.nsumblk = 0; 135 err = nilfs_segbuf_extend_segsum(segbuf); 136 if (unlikely(err)) 137 return err; 138 139 segbuf->sb_sum.flags = flags; 140 segbuf->sb_sum.sumbytes = sizeof(struct nilfs_segment_summary); 141 segbuf->sb_sum.nfinfo = segbuf->sb_sum.nfileblk = 0; 142 segbuf->sb_sum.ctime = ctime; 143 segbuf->sb_sum.cno = cno; 144 return 0; 145 } 146 147 /* 148 * Setup segment summary 149 */ 150 void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer *segbuf) 151 { 152 struct nilfs_segment_summary *raw_sum; 153 struct buffer_head *bh_sum; 154 155 bh_sum = list_entry(segbuf->sb_segsum_buffers.next, 156 struct buffer_head, b_assoc_buffers); 157 raw_sum = (struct nilfs_segment_summary *)bh_sum->b_data; 158 159 raw_sum->ss_magic = cpu_to_le32(NILFS_SEGSUM_MAGIC); 160 raw_sum->ss_bytes = cpu_to_le16(sizeof(*raw_sum)); 161 raw_sum->ss_flags = cpu_to_le16(segbuf->sb_sum.flags); 162 raw_sum->ss_seq = cpu_to_le64(segbuf->sb_sum.seg_seq); 163 raw_sum->ss_create = cpu_to_le64(segbuf->sb_sum.ctime); 164 raw_sum->ss_next = cpu_to_le64(segbuf->sb_sum.next); 165 raw_sum->ss_nblocks = cpu_to_le32(segbuf->sb_sum.nblocks); 166 raw_sum->ss_nfinfo = cpu_to_le32(segbuf->sb_sum.nfinfo); 167 raw_sum->ss_sumbytes = cpu_to_le32(segbuf->sb_sum.sumbytes); 168 raw_sum->ss_pad = 0; 169 raw_sum->ss_cno = cpu_to_le64(segbuf->sb_sum.cno); 170 } 171 172 /* 173 * CRC calculation routines 174 */ 175 static void 176 nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer *segbuf, u32 seed) 177 { 178 struct buffer_head *bh; 179 struct nilfs_segment_summary *raw_sum; 180 unsigned long size, bytes = segbuf->sb_sum.sumbytes; 181 u32 crc; 182 183 bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head, 184 b_assoc_buffers); 185 186 raw_sum = (struct nilfs_segment_summary *)bh->b_data; 187 size = min_t(unsigned long, bytes, bh->b_size); 188 crc = crc32_le(seed, 189 (unsigned char *)raw_sum + 190 sizeof(raw_sum->ss_datasum) + sizeof(raw_sum->ss_sumsum), 191 size - (sizeof(raw_sum->ss_datasum) + 192 sizeof(raw_sum->ss_sumsum))); 193 194 list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers, 195 b_assoc_buffers) { 196 bytes -= size; 197 size = min_t(unsigned long, bytes, bh->b_size); 198 crc = crc32_le(crc, bh->b_data, size); 199 } 200 raw_sum->ss_sumsum = cpu_to_le32(crc); 201 } 202 203 static void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer *segbuf, 204 u32 seed) 205 { 206 struct buffer_head *bh; 207 struct nilfs_segment_summary *raw_sum; 208 u32 crc; 209 210 bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head, 211 b_assoc_buffers); 212 raw_sum = (struct nilfs_segment_summary *)bh->b_data; 213 crc = crc32_le(seed, 214 (unsigned char *)raw_sum + sizeof(raw_sum->ss_datasum), 215 bh->b_size - sizeof(raw_sum->ss_datasum)); 216 217 list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers, 218 b_assoc_buffers) { 219 crc = crc32_le(crc, bh->b_data, bh->b_size); 220 } 221 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) { 222 size_t offset = offset_in_folio(bh->b_folio, bh->b_data); 223 unsigned char *from; 224 225 /* Do not support block sizes larger than PAGE_SIZE */ 226 from = kmap_local_folio(bh->b_folio, offset); 227 crc = crc32_le(crc, from, bh->b_size); 228 kunmap_local(from); 229 } 230 raw_sum->ss_datasum = cpu_to_le32(crc); 231 } 232 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; 238 struct the_nilfs *nilfs = segbuf->sb_super->s_fs_info; 239 unsigned int srsize; 240 u32 crc; 241 242 raw_sr = (struct nilfs_super_root *)segbuf->sb_super_root->b_data; 243 srsize = NILFS_SR_BYTES(nilfs->ns_inode_size); 244 crc = crc32_le(seed, 245 (unsigned char *)raw_sr + sizeof(raw_sr->sr_sum), 246 srsize - sizeof(raw_sr->sr_sum)); 247 raw_sr->sr_sum = cpu_to_le32(crc); 248 } 249 250 static void nilfs_release_buffers(struct list_head *list) 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); 256 brelse(bh); 257 } 258 } 259 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); 264 segbuf->sb_super_root = NULL; 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 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 304 int nilfs_wait_on_logs(struct list_head *logs) 305 { 306 struct nilfs_segment_buffer *segbuf; 307 int err, ret = 0; 308 309 list_for_each_entry(segbuf, logs, sb_list) { 310 err = nilfs_segbuf_wait(segbuf); 311 if (err && !ret) 312 ret = err; 313 } 314 return ret; 315 } 316 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 334 /* 335 * BIO operations 336 */ 337 static void nilfs_end_bio_write(struct bio *bio) 338 { 339 struct nilfs_segment_buffer *segbuf = bio->bi_private; 340 341 if (bio->bi_status) 342 atomic_inc(&segbuf->sb_err); 343 344 bio_put(bio); 345 complete(&segbuf->sb_bio_event); 346 } 347 348 static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer *segbuf, 349 struct nilfs_write_info *wi) 350 { 351 struct bio *bio = wi->bio; 352 353 bio->bi_end_io = nilfs_end_bio_write; 354 bio->bi_private = segbuf; 355 submit_bio(bio); 356 segbuf->sb_nbio++; 357 358 wi->bio = NULL; 359 wi->rest_blocks -= wi->end - wi->start; 360 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks); 361 wi->start = wi->end; 362 return 0; 363 } 364 365 static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf, 366 struct nilfs_write_info *wi) 367 { 368 wi->bio = NULL; 369 wi->rest_blocks = segbuf->sb_sum.nblocks; 370 wi->max_pages = BIO_MAX_VECS; 371 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks); 372 wi->start = wi->end = 0; 373 wi->blocknr = segbuf->sb_pseg_start; 374 } 375 376 static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer *segbuf, 377 struct nilfs_write_info *wi, 378 struct buffer_head *bh) 379 { 380 int err; 381 382 BUG_ON(wi->nr_vecs <= 0); 383 repeat: 384 if (!wi->bio) { 385 wi->bio = bio_alloc(wi->nilfs->ns_bdev, wi->nr_vecs, 386 REQ_OP_WRITE, GFP_NOIO); 387 wi->bio->bi_iter.bi_sector = (wi->blocknr + wi->end) << 388 (wi->nilfs->ns_blocksize_bits - 9); 389 } 390 391 if (bio_add_folio(wi->bio, bh->b_folio, bh->b_size, 392 offset_in_folio(bh->b_folio, bh->b_data))) { 393 wi->end++; 394 return 0; 395 } 396 /* bio is FULL */ 397 err = nilfs_segbuf_submit_bio(segbuf, wi); 398 /* never submit current bh */ 399 if (likely(!err)) 400 goto repeat; 401 return err; 402 } 403 404 /** 405 * nilfs_segbuf_write - submit write requests of a log 406 * @segbuf: buffer storing a log to be written 407 * @nilfs: nilfs object 408 * 409 * Return Value: On Success, 0 is returned. On Error, one of the following 410 * negative error code is returned. 411 * 412 * %-EIO - I/O error 413 * 414 * %-ENOMEM - Insufficient memory available. 415 */ 416 static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf, 417 struct the_nilfs *nilfs) 418 { 419 struct nilfs_write_info wi; 420 struct buffer_head *bh; 421 int res = 0; 422 423 wi.nilfs = nilfs; 424 nilfs_segbuf_prepare_write(segbuf, &wi); 425 426 list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) { 427 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh); 428 if (unlikely(res)) 429 goto failed_bio; 430 } 431 432 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) { 433 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh); 434 if (unlikely(res)) 435 goto failed_bio; 436 } 437 438 if (wi.bio) { 439 /* 440 * Last BIO is always sent through the following 441 * submission. 442 */ 443 wi.bio->bi_opf |= REQ_SYNC; 444 res = nilfs_segbuf_submit_bio(segbuf, &wi); 445 } 446 447 failed_bio: 448 return res; 449 } 450 451 /** 452 * nilfs_segbuf_wait - wait for completion of requested BIOs 453 * @segbuf: segment buffer 454 * 455 * Return Value: On Success, 0 is returned. On Error, one of the following 456 * negative error code is returned. 457 * 458 * %-EIO - I/O error 459 */ 460 static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf) 461 { 462 int err = 0; 463 464 if (!segbuf->sb_nbio) 465 return 0; 466 467 do { 468 wait_for_completion(&segbuf->sb_bio_event); 469 } while (--segbuf->sb_nbio > 0); 470 471 if (unlikely(atomic_read(&segbuf->sb_err) > 0)) { 472 nilfs_err(segbuf->sb_super, 473 "I/O error writing log (start-blocknr=%llu, block-count=%lu) in segment %llu", 474 (unsigned long long)segbuf->sb_pseg_start, 475 segbuf->sb_sum.nblocks, 476 (unsigned long long)segbuf->sb_segnum); 477 err = -EIO; 478 } 479 return err; 480 } 481