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
nilfs_segbuf_new(struct super_block * sb)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
nilfs_segbuf_free(struct nilfs_segment_buffer * segbuf)55 void nilfs_segbuf_free(struct nilfs_segment_buffer *segbuf)
56 {
57 kmem_cache_free(nilfs_segbuf_cachep, segbuf);
58 }
59
nilfs_segbuf_map(struct nilfs_segment_buffer * segbuf,__u64 segnum,unsigned long offset,struct the_nilfs * nilfs)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 */
nilfs_segbuf_map_cont(struct nilfs_segment_buffer * segbuf,struct nilfs_segment_buffer * prev)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
nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer * segbuf,__u64 nextnum,struct the_nilfs * nilfs)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
nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer * segbuf)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
nilfs_segbuf_extend_payload(struct nilfs_segment_buffer * segbuf,struct buffer_head ** bhp)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
nilfs_segbuf_reset(struct nilfs_segment_buffer * segbuf,unsigned int flags,time64_t ctime,__u64 cno)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 */
nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer * segbuf)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
nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer * segbuf,u32 seed)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
nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer * segbuf,u32 seed)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 void *kaddr;
209 u32 crc;
210
211 bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head,
212 b_assoc_buffers);
213 raw_sum = (struct nilfs_segment_summary *)bh->b_data;
214 crc = crc32_le(seed,
215 (unsigned char *)raw_sum + sizeof(raw_sum->ss_datasum),
216 bh->b_size - sizeof(raw_sum->ss_datasum));
217
218 list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers,
219 b_assoc_buffers) {
220 crc = crc32_le(crc, bh->b_data, bh->b_size);
221 }
222 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
223 kaddr = kmap_atomic(bh->b_page);
224 crc = crc32_le(crc, kaddr + bh_offset(bh), bh->b_size);
225 kunmap_atomic(kaddr);
226 }
227 raw_sum->ss_datasum = cpu_to_le32(crc);
228 }
229
230 static void
nilfs_segbuf_fill_in_super_root_crc(struct nilfs_segment_buffer * segbuf,u32 seed)231 nilfs_segbuf_fill_in_super_root_crc(struct nilfs_segment_buffer *segbuf,
232 u32 seed)
233 {
234 struct nilfs_super_root *raw_sr;
235 struct the_nilfs *nilfs = segbuf->sb_super->s_fs_info;
236 unsigned int srsize;
237 u32 crc;
238
239 raw_sr = (struct nilfs_super_root *)segbuf->sb_super_root->b_data;
240 srsize = NILFS_SR_BYTES(nilfs->ns_inode_size);
241 crc = crc32_le(seed,
242 (unsigned char *)raw_sr + sizeof(raw_sr->sr_sum),
243 srsize - sizeof(raw_sr->sr_sum));
244 raw_sr->sr_sum = cpu_to_le32(crc);
245 }
246
nilfs_release_buffers(struct list_head * list)247 static void nilfs_release_buffers(struct list_head *list)
248 {
249 struct buffer_head *bh, *n;
250
251 list_for_each_entry_safe(bh, n, list, b_assoc_buffers) {
252 list_del_init(&bh->b_assoc_buffers);
253 brelse(bh);
254 }
255 }
256
nilfs_segbuf_clear(struct nilfs_segment_buffer * segbuf)257 static void nilfs_segbuf_clear(struct nilfs_segment_buffer *segbuf)
258 {
259 nilfs_release_buffers(&segbuf->sb_segsum_buffers);
260 nilfs_release_buffers(&segbuf->sb_payload_buffers);
261 segbuf->sb_super_root = NULL;
262 }
263
264 /*
265 * Iterators for segment buffers
266 */
nilfs_clear_logs(struct list_head * logs)267 void nilfs_clear_logs(struct list_head *logs)
268 {
269 struct nilfs_segment_buffer *segbuf;
270
271 list_for_each_entry(segbuf, logs, sb_list)
272 nilfs_segbuf_clear(segbuf);
273 }
274
nilfs_truncate_logs(struct list_head * logs,struct nilfs_segment_buffer * last)275 void nilfs_truncate_logs(struct list_head *logs,
276 struct nilfs_segment_buffer *last)
277 {
278 struct nilfs_segment_buffer *n, *segbuf;
279
280 segbuf = list_prepare_entry(last, logs, sb_list);
281 list_for_each_entry_safe_continue(segbuf, n, logs, sb_list) {
282 list_del_init(&segbuf->sb_list);
283 nilfs_segbuf_clear(segbuf);
284 nilfs_segbuf_free(segbuf);
285 }
286 }
287
nilfs_write_logs(struct list_head * logs,struct the_nilfs * nilfs)288 int nilfs_write_logs(struct list_head *logs, struct the_nilfs *nilfs)
289 {
290 struct nilfs_segment_buffer *segbuf;
291 int ret = 0;
292
293 list_for_each_entry(segbuf, logs, sb_list) {
294 ret = nilfs_segbuf_write(segbuf, nilfs);
295 if (ret)
296 break;
297 }
298 return ret;
299 }
300
nilfs_wait_on_logs(struct list_head * logs)301 int nilfs_wait_on_logs(struct list_head *logs)
302 {
303 struct nilfs_segment_buffer *segbuf;
304 int err, ret = 0;
305
306 list_for_each_entry(segbuf, logs, sb_list) {
307 err = nilfs_segbuf_wait(segbuf);
308 if (err && !ret)
309 ret = err;
310 }
311 return ret;
312 }
313
314 /**
315 * nilfs_add_checksums_on_logs - add checksums on the logs
316 * @logs: list of segment buffers storing target logs
317 * @seed: checksum seed value
318 */
nilfs_add_checksums_on_logs(struct list_head * logs,u32 seed)319 void nilfs_add_checksums_on_logs(struct list_head *logs, u32 seed)
320 {
321 struct nilfs_segment_buffer *segbuf;
322
323 list_for_each_entry(segbuf, logs, sb_list) {
324 if (segbuf->sb_super_root)
325 nilfs_segbuf_fill_in_super_root_crc(segbuf, seed);
326 nilfs_segbuf_fill_in_segsum_crc(segbuf, seed);
327 nilfs_segbuf_fill_in_data_crc(segbuf, seed);
328 }
329 }
330
331 /*
332 * BIO operations
333 */
nilfs_end_bio_write(struct bio * bio)334 static void nilfs_end_bio_write(struct bio *bio)
335 {
336 struct nilfs_segment_buffer *segbuf = bio->bi_private;
337
338 if (bio->bi_status)
339 atomic_inc(&segbuf->sb_err);
340
341 bio_put(bio);
342 complete(&segbuf->sb_bio_event);
343 }
344
nilfs_segbuf_submit_bio(struct nilfs_segment_buffer * segbuf,struct nilfs_write_info * wi,int mode,int mode_flags)345 static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer *segbuf,
346 struct nilfs_write_info *wi, int mode,
347 int mode_flags)
348 {
349 struct bio *bio = wi->bio;
350 int err;
351
352 if (segbuf->sb_nbio > 0 &&
353 bdi_write_congested(segbuf->sb_super->s_bdi)) {
354 wait_for_completion(&segbuf->sb_bio_event);
355 segbuf->sb_nbio--;
356 if (unlikely(atomic_read(&segbuf->sb_err))) {
357 bio_put(bio);
358 err = -EIO;
359 goto failed;
360 }
361 }
362
363 bio->bi_end_io = nilfs_end_bio_write;
364 bio->bi_private = segbuf;
365 bio_set_op_attrs(bio, mode, mode_flags);
366 submit_bio(bio);
367 segbuf->sb_nbio++;
368
369 wi->bio = NULL;
370 wi->rest_blocks -= wi->end - wi->start;
371 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
372 wi->start = wi->end;
373 return 0;
374
375 failed:
376 wi->bio = NULL;
377 return err;
378 }
379
380 /**
381 * nilfs_alloc_seg_bio - allocate a new bio for writing log
382 * @nilfs: nilfs object
383 * @start: start block number of the bio
384 * @nr_vecs: request size of page vector.
385 *
386 * Return Value: On success, pointer to the struct bio is returned.
387 * On error, NULL is returned.
388 */
nilfs_alloc_seg_bio(struct the_nilfs * nilfs,sector_t start,int nr_vecs)389 static struct bio *nilfs_alloc_seg_bio(struct the_nilfs *nilfs, sector_t start,
390 int nr_vecs)
391 {
392 struct bio *bio;
393
394 bio = bio_alloc(GFP_NOIO, nr_vecs);
395 if (bio == NULL) {
396 while (!bio && (nr_vecs >>= 1))
397 bio = bio_alloc(GFP_NOIO, nr_vecs);
398 }
399 if (likely(bio)) {
400 bio_set_dev(bio, nilfs->ns_bdev);
401 bio->bi_iter.bi_sector =
402 start << (nilfs->ns_blocksize_bits - 9);
403 }
404 return bio;
405 }
406
nilfs_segbuf_prepare_write(struct nilfs_segment_buffer * segbuf,struct nilfs_write_info * wi)407 static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf,
408 struct nilfs_write_info *wi)
409 {
410 wi->bio = NULL;
411 wi->rest_blocks = segbuf->sb_sum.nblocks;
412 wi->max_pages = BIO_MAX_PAGES;
413 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
414 wi->start = wi->end = 0;
415 wi->blocknr = segbuf->sb_pseg_start;
416 }
417
nilfs_segbuf_submit_bh(struct nilfs_segment_buffer * segbuf,struct nilfs_write_info * wi,struct buffer_head * bh,int mode)418 static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer *segbuf,
419 struct nilfs_write_info *wi,
420 struct buffer_head *bh, int mode)
421 {
422 int len, err;
423
424 BUG_ON(wi->nr_vecs <= 0);
425 repeat:
426 if (!wi->bio) {
427 wi->bio = nilfs_alloc_seg_bio(wi->nilfs, wi->blocknr + wi->end,
428 wi->nr_vecs);
429 if (unlikely(!wi->bio))
430 return -ENOMEM;
431 }
432
433 len = bio_add_page(wi->bio, bh->b_page, bh->b_size, bh_offset(bh));
434 if (len == bh->b_size) {
435 wi->end++;
436 return 0;
437 }
438 /* bio is FULL */
439 err = nilfs_segbuf_submit_bio(segbuf, wi, mode, 0);
440 /* never submit current bh */
441 if (likely(!err))
442 goto repeat;
443 return err;
444 }
445
446 /**
447 * nilfs_segbuf_write - submit write requests of a log
448 * @segbuf: buffer storing a log to be written
449 * @nilfs: nilfs object
450 *
451 * Return Value: On Success, 0 is returned. On Error, one of the following
452 * negative error code is returned.
453 *
454 * %-EIO - I/O error
455 *
456 * %-ENOMEM - Insufficient memory available.
457 */
nilfs_segbuf_write(struct nilfs_segment_buffer * segbuf,struct the_nilfs * nilfs)458 static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
459 struct the_nilfs *nilfs)
460 {
461 struct nilfs_write_info wi;
462 struct buffer_head *bh;
463 int res = 0;
464
465 wi.nilfs = nilfs;
466 nilfs_segbuf_prepare_write(segbuf, &wi);
467
468 list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) {
469 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, REQ_OP_WRITE);
470 if (unlikely(res))
471 goto failed_bio;
472 }
473
474 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
475 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, REQ_OP_WRITE);
476 if (unlikely(res))
477 goto failed_bio;
478 }
479
480 if (wi.bio) {
481 /*
482 * Last BIO is always sent through the following
483 * submission.
484 */
485 res = nilfs_segbuf_submit_bio(segbuf, &wi, REQ_OP_WRITE,
486 REQ_SYNC);
487 }
488
489 failed_bio:
490 return res;
491 }
492
493 /**
494 * nilfs_segbuf_wait - wait for completion of requested BIOs
495 * @segbuf: segment buffer
496 *
497 * Return Value: On Success, 0 is returned. On Error, one of the following
498 * negative error code is returned.
499 *
500 * %-EIO - I/O error
501 */
nilfs_segbuf_wait(struct nilfs_segment_buffer * segbuf)502 static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf)
503 {
504 int err = 0;
505
506 if (!segbuf->sb_nbio)
507 return 0;
508
509 do {
510 wait_for_completion(&segbuf->sb_bio_event);
511 } while (--segbuf->sb_nbio > 0);
512
513 if (unlikely(atomic_read(&segbuf->sb_err) > 0)) {
514 nilfs_err(segbuf->sb_super,
515 "I/O error writing log (start-blocknr=%llu, block-count=%lu) in segment %llu",
516 (unsigned long long)segbuf->sb_pseg_start,
517 segbuf->sb_sum.nblocks,
518 (unsigned long long)segbuf->sb_segnum);
519 err = -EIO;
520 }
521 return err;
522 }
523