USB: serial: f81232: fix interrupt worker not stop
[linux/fpc-iii.git] / fs / nilfs2 / segbuf.c
blob20c479b5e41b8c7a9bbd09d309cf33aa51e3a90d
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * segbuf.c - NILFS segment buffer
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
7 * Written by Ryusuke Konishi.
9 */
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"
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;
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);
34 struct nilfs_segment_buffer *nilfs_segbuf_new(struct super_block *sb)
36 struct nilfs_segment_buffer *segbuf;
38 segbuf = kmem_cache_alloc(nilfs_segbuf_cachep, GFP_NOFS);
39 if (unlikely(!segbuf))
40 return NULL;
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;
48 init_completion(&segbuf->sb_bio_event);
49 atomic_set(&segbuf->sb_err, 0);
50 segbuf->sb_nbio = 0;
52 return segbuf;
55 void nilfs_segbuf_free(struct nilfs_segment_buffer *segbuf)
57 kmem_cache_free(nilfs_segbuf_cachep, segbuf);
60 void nilfs_segbuf_map(struct nilfs_segment_buffer *segbuf, __u64 segnum,
61 unsigned long offset, struct the_nilfs *nilfs)
63 segbuf->sb_segnum = segnum;
64 nilfs_get_segment_range(nilfs, segnum, &segbuf->sb_fseg_start,
65 &segbuf->sb_fseg_end);
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;
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
77 void nilfs_segbuf_map_cont(struct nilfs_segment_buffer *segbuf,
78 struct nilfs_segment_buffer *prev)
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;
88 void nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer *segbuf,
89 __u64 nextnum, struct the_nilfs *nilfs)
91 segbuf->sb_nextnum = nextnum;
92 segbuf->sb_sum.next = nilfs_get_segment_start_blocknr(nilfs, nextnum);
95 int nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer *segbuf)
97 struct buffer_head *bh;
99 bh = sb_getblk(segbuf->sb_super,
100 segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk);
101 if (unlikely(!bh))
102 return -ENOMEM;
104 nilfs_segbuf_add_segsum_buffer(segbuf, bh);
105 return 0;
108 int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer *segbuf,
109 struct buffer_head **bhp)
111 struct buffer_head *bh;
113 bh = sb_getblk(segbuf->sb_super,
114 segbuf->sb_pseg_start + segbuf->sb_sum.nblocks);
115 if (unlikely(!bh))
116 return -ENOMEM;
118 nilfs_segbuf_add_payload_buffer(segbuf, bh);
119 *bhp = bh;
120 return 0;
123 int nilfs_segbuf_reset(struct nilfs_segment_buffer *segbuf, unsigned int flags,
124 time64_t ctime, __u64 cno)
126 int err;
128 segbuf->sb_sum.nblocks = segbuf->sb_sum.nsumblk = 0;
129 err = nilfs_segbuf_extend_segsum(segbuf);
130 if (unlikely(err))
131 return err;
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;
142 * Setup segment summary
144 void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer *segbuf)
146 struct nilfs_segment_summary *raw_sum;
147 struct buffer_head *bh_sum;
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;
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);
167 * CRC calculation routines
169 static void
170 nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer *segbuf, u32 seed)
172 struct buffer_head *bh;
173 struct nilfs_segment_summary *raw_sum;
174 unsigned long size, bytes = segbuf->sb_sum.sumbytes;
175 u32 crc;
177 bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head,
178 b_assoc_buffers);
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)));
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);
194 raw_sum->ss_sumsum = cpu_to_le32(crc);
197 static void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer *segbuf,
198 u32 seed)
200 struct buffer_head *bh;
201 struct nilfs_segment_summary *raw_sum;
202 void *kaddr;
203 u32 crc;
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));
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);
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);
221 raw_sum->ss_datasum = cpu_to_le32(crc);
224 static void
225 nilfs_segbuf_fill_in_super_root_crc(struct nilfs_segment_buffer *segbuf,
226 u32 seed)
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;
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);
241 static void nilfs_release_buffers(struct list_head *list)
243 struct buffer_head *bh, *n;
245 list_for_each_entry_safe(bh, n, list, b_assoc_buffers) {
246 list_del_init(&bh->b_assoc_buffers);
247 brelse(bh);
251 static void nilfs_segbuf_clear(struct nilfs_segment_buffer *segbuf)
253 nilfs_release_buffers(&segbuf->sb_segsum_buffers);
254 nilfs_release_buffers(&segbuf->sb_payload_buffers);
255 segbuf->sb_super_root = NULL;
259 * Iterators for segment buffers
261 void nilfs_clear_logs(struct list_head *logs)
263 struct nilfs_segment_buffer *segbuf;
265 list_for_each_entry(segbuf, logs, sb_list)
266 nilfs_segbuf_clear(segbuf);
269 void nilfs_truncate_logs(struct list_head *logs,
270 struct nilfs_segment_buffer *last)
272 struct nilfs_segment_buffer *n, *segbuf;
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);
282 int nilfs_write_logs(struct list_head *logs, struct the_nilfs *nilfs)
284 struct nilfs_segment_buffer *segbuf;
285 int ret = 0;
287 list_for_each_entry(segbuf, logs, sb_list) {
288 ret = nilfs_segbuf_write(segbuf, nilfs);
289 if (ret)
290 break;
292 return ret;
295 int nilfs_wait_on_logs(struct list_head *logs)
297 struct nilfs_segment_buffer *segbuf;
298 int err, ret = 0;
300 list_for_each_entry(segbuf, logs, sb_list) {
301 err = nilfs_segbuf_wait(segbuf);
302 if (err && !ret)
303 ret = err;
305 return ret;
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
313 void nilfs_add_checksums_on_logs(struct list_head *logs, u32 seed)
315 struct nilfs_segment_buffer *segbuf;
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);
326 * BIO operations
328 static void nilfs_end_bio_write(struct bio *bio)
330 struct nilfs_segment_buffer *segbuf = bio->bi_private;
332 if (bio->bi_status)
333 atomic_inc(&segbuf->sb_err);
335 bio_put(bio);
336 complete(&segbuf->sb_bio_event);
339 static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer *segbuf,
340 struct nilfs_write_info *wi, int mode,
341 int mode_flags)
343 struct bio *bio = wi->bio;
344 int err;
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;
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++;
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;
369 failed:
370 wi->bio = NULL;
371 return err;
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.
380 * Return Value: On success, pointer to the struct bio is returned.
381 * On error, NULL is returned.
383 static struct bio *nilfs_alloc_seg_bio(struct the_nilfs *nilfs, sector_t start,
384 int nr_vecs)
386 struct bio *bio;
388 bio = bio_alloc(GFP_NOIO, nr_vecs);
389 if (bio == NULL) {
390 while (!bio && (nr_vecs >>= 1))
391 bio = bio_alloc(GFP_NOIO, nr_vecs);
393 if (likely(bio)) {
394 bio_set_dev(bio, nilfs->ns_bdev);
395 bio->bi_iter.bi_sector =
396 start << (nilfs->ns_blocksize_bits - 9);
398 return bio;
401 static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf,
402 struct nilfs_write_info *wi)
404 wi->bio = NULL;
405 wi->rest_blocks = segbuf->sb_sum.nblocks;
406 wi->max_pages = BIO_MAX_PAGES;
407 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
408 wi->start = wi->end = 0;
409 wi->blocknr = segbuf->sb_pseg_start;
412 static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer *segbuf,
413 struct nilfs_write_info *wi,
414 struct buffer_head *bh, int mode)
416 int len, err;
418 BUG_ON(wi->nr_vecs <= 0);
419 repeat:
420 if (!wi->bio) {
421 wi->bio = nilfs_alloc_seg_bio(wi->nilfs, wi->blocknr + wi->end,
422 wi->nr_vecs);
423 if (unlikely(!wi->bio))
424 return -ENOMEM;
427 len = bio_add_page(wi->bio, bh->b_page, bh->b_size, bh_offset(bh));
428 if (len == bh->b_size) {
429 wi->end++;
430 return 0;
432 /* bio is FULL */
433 err = nilfs_segbuf_submit_bio(segbuf, wi, mode, 0);
434 /* never submit current bh */
435 if (likely(!err))
436 goto repeat;
437 return err;
441 * nilfs_segbuf_write - submit write requests of a log
442 * @segbuf: buffer storing a log to be written
443 * @nilfs: nilfs object
445 * Return Value: On Success, 0 is returned. On Error, one of the following
446 * negative error code is returned.
448 * %-EIO - I/O error
450 * %-ENOMEM - Insufficient memory available.
452 static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
453 struct the_nilfs *nilfs)
455 struct nilfs_write_info wi;
456 struct buffer_head *bh;
457 int res = 0;
459 wi.nilfs = nilfs;
460 nilfs_segbuf_prepare_write(segbuf, &wi);
462 list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) {
463 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, REQ_OP_WRITE);
464 if (unlikely(res))
465 goto failed_bio;
468 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
469 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, REQ_OP_WRITE);
470 if (unlikely(res))
471 goto failed_bio;
474 if (wi.bio) {
476 * Last BIO is always sent through the following
477 * submission.
479 res = nilfs_segbuf_submit_bio(segbuf, &wi, REQ_OP_WRITE,
480 REQ_SYNC);
483 failed_bio:
484 return res;
488 * nilfs_segbuf_wait - wait for completion of requested BIOs
489 * @segbuf: segment buffer
491 * Return Value: On Success, 0 is returned. On Error, one of the following
492 * negative error code is returned.
494 * %-EIO - I/O error
496 static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf)
498 int err = 0;
500 if (!segbuf->sb_nbio)
501 return 0;
503 do {
504 wait_for_completion(&segbuf->sb_bio_event);
505 } while (--segbuf->sb_nbio > 0);
507 if (unlikely(atomic_read(&segbuf->sb_err) > 0)) {
508 nilfs_msg(segbuf->sb_super, KERN_ERR,
509 "I/O error writing log (start-blocknr=%llu, block-count=%lu) in segment %llu",
510 (unsigned long long)segbuf->sb_pseg_start,
511 segbuf->sb_sum.nblocks,
512 (unsigned long long)segbuf->sb_segnum);
513 err = -EIO;
515 return err;