GNU Linux-libre 4.19.211-gnu1
[releases.git] / fs / nilfs2 / segbuf.c
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 (bio == NULL) {
390                 while (!bio && (nr_vecs >>= 1))
391                         bio = bio_alloc(GFP_NOIO, nr_vecs);
392         }
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);
397         }
398         return bio;
399 }
400
401 static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf,
402                                        struct nilfs_write_info *wi)
403 {
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;
410 }
411
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)
415 {
416         int len, err;
417
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;
425         }
426
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;
431         }
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;
438 }
439
440 /**
441  * nilfs_segbuf_write - submit write requests of a log
442  * @segbuf: buffer storing a log to be written
443  * @nilfs: nilfs object
444  *
445  * Return Value: On Success, 0 is returned. On Error, one of the following
446  * negative error code is returned.
447  *
448  * %-EIO - I/O error
449  *
450  * %-ENOMEM - Insufficient memory available.
451  */
452 static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
453                               struct the_nilfs *nilfs)
454 {
455         struct nilfs_write_info wi;
456         struct buffer_head *bh;
457         int res = 0;
458
459         wi.nilfs = nilfs;
460         nilfs_segbuf_prepare_write(segbuf, &wi);
461
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;
466         }
467
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;
472         }
473
474         if (wi.bio) {
475                 /*
476                  * Last BIO is always sent through the following
477                  * submission.
478                  */
479                 res = nilfs_segbuf_submit_bio(segbuf, &wi, REQ_OP_WRITE,
480                                               REQ_SYNC);
481         }
482
483  failed_bio:
484         return res;
485 }
486
487 /**
488  * nilfs_segbuf_wait - wait for completion of requested BIOs
489  * @segbuf: segment buffer
490  *
491  * Return Value: On Success, 0 is returned. On Error, one of the following
492  * negative error code is returned.
493  *
494  * %-EIO - I/O error
495  */
496 static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf)
497 {
498         int err = 0;
499
500         if (!segbuf->sb_nbio)
501                 return 0;
502
503         do {
504                 wait_for_completion(&segbuf->sb_bio_event);
505         } while (--segbuf->sb_nbio > 0);
506
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;
514         }
515         return err;
516 }