blob: 6f87b2ac1aeb001c4e6c73997e584b5894b67a6c [file] [log] [blame]
Ryusuke Konishi64b5a322009-04-06 19:01:36 -07001/*
2 * segbuf.c - NILFS segment buffer
3 *
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -070016 * Written by Ryusuke Konishi.
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070017 *
18 */
19
20#include <linux/buffer_head.h>
21#include <linux/writeback.h>
22#include <linux/crc32.h>
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +090023#include <linux/backing-dev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070025#include "page.h"
26#include "segbuf.h"
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070027
28
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +090029struct nilfs_write_info {
30 struct the_nilfs *nilfs;
31 struct bio *bio;
Ryusuke Konishi55480a02010-03-14 03:55:56 +090032 int start, end; /* The region to be submitted */
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +090033 int rest_blocks;
34 int max_pages;
35 int nr_vecs;
36 sector_t blocknr;
37};
38
Ryusuke Konishid1c6b722009-12-17 00:55:40 +090039static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
40 struct the_nilfs *nilfs);
41static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf);
42
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070043struct nilfs_segment_buffer *nilfs_segbuf_new(struct super_block *sb)
44{
45 struct nilfs_segment_buffer *segbuf;
46
47 segbuf = kmem_cache_alloc(nilfs_segbuf_cachep, GFP_NOFS);
48 if (unlikely(!segbuf))
49 return NULL;
50
51 segbuf->sb_super = sb;
52 INIT_LIST_HEAD(&segbuf->sb_list);
53 INIT_LIST_HEAD(&segbuf->sb_segsum_buffers);
54 INIT_LIST_HEAD(&segbuf->sb_payload_buffers);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +090055 segbuf->sb_super_root = NULL;
Ryusuke Konishi9284ad22009-11-25 01:04:21 +090056
57 init_completion(&segbuf->sb_bio_event);
58 atomic_set(&segbuf->sb_err, 0);
59 segbuf->sb_nbio = 0;
60
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070061 return segbuf;
62}
63
64void nilfs_segbuf_free(struct nilfs_segment_buffer *segbuf)
65{
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070066 kmem_cache_free(nilfs_segbuf_cachep, segbuf);
67}
68
Ryusuke Konishicece5522009-04-06 19:01:58 -070069void nilfs_segbuf_map(struct nilfs_segment_buffer *segbuf, __u64 segnum,
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070070 unsigned long offset, struct the_nilfs *nilfs)
71{
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070072 segbuf->sb_segnum = segnum;
73 nilfs_get_segment_range(nilfs, segnum, &segbuf->sb_fseg_start,
74 &segbuf->sb_fseg_end);
75
76 segbuf->sb_pseg_start = segbuf->sb_fseg_start + offset;
77 segbuf->sb_rest_blocks =
78 segbuf->sb_fseg_end - segbuf->sb_pseg_start + 1;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070079}
80
Ryusuke Konishia694291a2009-11-29 23:03:04 +090081/**
82 * nilfs_segbuf_map_cont - map a new log behind a given log
83 * @segbuf: new segment buffer
84 * @prev: segment buffer containing a log to be continued
85 */
86void nilfs_segbuf_map_cont(struct nilfs_segment_buffer *segbuf,
87 struct nilfs_segment_buffer *prev)
88{
89 segbuf->sb_segnum = prev->sb_segnum;
90 segbuf->sb_fseg_start = prev->sb_fseg_start;
91 segbuf->sb_fseg_end = prev->sb_fseg_end;
92 segbuf->sb_pseg_start = prev->sb_pseg_start + prev->sb_sum.nblocks;
93 segbuf->sb_rest_blocks =
94 segbuf->sb_fseg_end - segbuf->sb_pseg_start + 1;
95}
96
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070097void nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer *segbuf,
98 __u64 nextnum, struct the_nilfs *nilfs)
99{
100 segbuf->sb_nextnum = nextnum;
101 segbuf->sb_sum.next = nilfs_get_segment_start_blocknr(nilfs, nextnum);
102}
103
104int nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer *segbuf)
105{
106 struct buffer_head *bh;
107
108 bh = sb_getblk(segbuf->sb_super,
109 segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk);
110 if (unlikely(!bh))
111 return -ENOMEM;
112
113 nilfs_segbuf_add_segsum_buffer(segbuf, bh);
114 return 0;
115}
116
117int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer *segbuf,
118 struct buffer_head **bhp)
119{
120 struct buffer_head *bh;
121
122 bh = sb_getblk(segbuf->sb_super,
123 segbuf->sb_pseg_start + segbuf->sb_sum.nblocks);
124 if (unlikely(!bh))
125 return -ENOMEM;
126
127 nilfs_segbuf_add_payload_buffer(segbuf, bh);
128 *bhp = bh;
129 return 0;
130}
131
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700132int nilfs_segbuf_reset(struct nilfs_segment_buffer *segbuf, unsigned int flags,
Ryusuke Konishi50614bc2010-04-10 17:59:15 +0900133 time_t ctime, __u64 cno)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700134{
135 int err;
136
137 segbuf->sb_sum.nblocks = segbuf->sb_sum.nsumblk = 0;
138 err = nilfs_segbuf_extend_segsum(segbuf);
139 if (unlikely(err))
140 return err;
141
142 segbuf->sb_sum.flags = flags;
143 segbuf->sb_sum.sumbytes = sizeof(struct nilfs_segment_summary);
144 segbuf->sb_sum.nfinfo = segbuf->sb_sum.nfileblk = 0;
145 segbuf->sb_sum.ctime = ctime;
Ryusuke Konishi50614bc2010-04-10 17:59:15 +0900146 segbuf->sb_sum.cno = cno;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700147 return 0;
148}
149
150/*
Ryusuke Konishi7a650042010-03-14 03:32:40 +0900151 * Setup segment summary
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700152 */
153void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer *segbuf)
154{
155 struct nilfs_segment_summary *raw_sum;
156 struct buffer_head *bh_sum;
157
158 bh_sum = list_entry(segbuf->sb_segsum_buffers.next,
159 struct buffer_head, b_assoc_buffers);
160 raw_sum = (struct nilfs_segment_summary *)bh_sum->b_data;
161
162 raw_sum->ss_magic = cpu_to_le32(NILFS_SEGSUM_MAGIC);
163 raw_sum->ss_bytes = cpu_to_le16(sizeof(*raw_sum));
164 raw_sum->ss_flags = cpu_to_le16(segbuf->sb_sum.flags);
165 raw_sum->ss_seq = cpu_to_le64(segbuf->sb_sum.seg_seq);
166 raw_sum->ss_create = cpu_to_le64(segbuf->sb_sum.ctime);
167 raw_sum->ss_next = cpu_to_le64(segbuf->sb_sum.next);
168 raw_sum->ss_nblocks = cpu_to_le32(segbuf->sb_sum.nblocks);
169 raw_sum->ss_nfinfo = cpu_to_le32(segbuf->sb_sum.nfinfo);
170 raw_sum->ss_sumbytes = cpu_to_le32(segbuf->sb_sum.sumbytes);
171 raw_sum->ss_pad = 0;
Ryusuke Konishi50614bc2010-04-10 17:59:15 +0900172 raw_sum->ss_cno = cpu_to_le64(segbuf->sb_sum.cno);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700173}
174
175/*
176 * CRC calculation routines
177 */
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900178static void
179nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer *segbuf, u32 seed)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700180{
181 struct buffer_head *bh;
182 struct nilfs_segment_summary *raw_sum;
183 unsigned long size, bytes = segbuf->sb_sum.sumbytes;
184 u32 crc;
185
186 bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head,
187 b_assoc_buffers);
188
189 raw_sum = (struct nilfs_segment_summary *)bh->b_data;
190 size = min_t(unsigned long, bytes, bh->b_size);
191 crc = crc32_le(seed,
192 (unsigned char *)raw_sum +
193 sizeof(raw_sum->ss_datasum) + sizeof(raw_sum->ss_sumsum),
194 size - (sizeof(raw_sum->ss_datasum) +
195 sizeof(raw_sum->ss_sumsum)));
196
197 list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers,
198 b_assoc_buffers) {
199 bytes -= size;
200 size = min_t(unsigned long, bytes, bh->b_size);
201 crc = crc32_le(crc, bh->b_data, size);
202 }
203 raw_sum->ss_sumsum = cpu_to_le32(crc);
204}
205
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900206static void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer *segbuf,
207 u32 seed)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700208{
209 struct buffer_head *bh;
210 struct nilfs_segment_summary *raw_sum;
211 void *kaddr;
212 u32 crc;
213
214 bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head,
215 b_assoc_buffers);
216 raw_sum = (struct nilfs_segment_summary *)bh->b_data;
217 crc = crc32_le(seed,
218 (unsigned char *)raw_sum + sizeof(raw_sum->ss_datasum),
219 bh->b_size - sizeof(raw_sum->ss_datasum));
220
221 list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers,
222 b_assoc_buffers) {
223 crc = crc32_le(crc, bh->b_data, bh->b_size);
224 }
225 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
Cong Wang7b9c0972011-11-25 23:14:33 +0800226 kaddr = kmap_atomic(bh->b_page);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700227 crc = crc32_le(crc, kaddr + bh_offset(bh), bh->b_size);
Cong Wang7b9c0972011-11-25 23:14:33 +0800228 kunmap_atomic(kaddr);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700229 }
230 raw_sum->ss_datasum = cpu_to_le32(crc);
231}
232
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900233static void
234nilfs_segbuf_fill_in_super_root_crc(struct nilfs_segment_buffer *segbuf,
235 u32 seed)
236{
237 struct nilfs_super_root *raw_sr;
Ryusuke Konishi6c6de1a2011-04-30 18:56:12 +0900238 struct the_nilfs *nilfs = segbuf->sb_super->s_fs_info;
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700239 unsigned int srsize;
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900240 u32 crc;
241
242 raw_sr = (struct nilfs_super_root *)segbuf->sb_super_root->b_data;
Ryusuke Konishi6c6de1a2011-04-30 18:56:12 +0900243 srsize = NILFS_SR_BYTES(nilfs->ns_inode_size);
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900244 crc = crc32_le(seed,
245 (unsigned char *)raw_sr + sizeof(raw_sr->sr_sum),
Ryusuke Konishi6c6de1a2011-04-30 18:56:12 +0900246 srsize - sizeof(raw_sr->sr_sum));
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900247 raw_sr->sr_sum = cpu_to_le32(crc);
248}
249
Ryusuke Konishie29df392009-11-29 16:51:16 +0900250static void nilfs_release_buffers(struct list_head *list)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700251{
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);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700256 brelse(bh);
257 }
258}
259
Ryusuke Konishie29df392009-11-29 16:51:16 +0900260static 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);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900264 segbuf->sb_super_root = NULL;
Ryusuke Konishie29df392009-11-29 16:51:16 +0900265}
266
267/*
268 * Iterators for segment buffers
269 */
270void 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
278void 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
Ryusuke Konishid1c6b722009-12-17 00:55:40 +0900291int 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
Ryusuke Konishie29df392009-11-29 16:51:16 +0900304int nilfs_wait_on_logs(struct list_head *logs)
305{
306 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishid0676332010-03-22 19:33:43 +0900307 int err, ret = 0;
Ryusuke Konishie29df392009-11-29 16:51:16 +0900308
309 list_for_each_entry(segbuf, logs, sb_list) {
310 err = nilfs_segbuf_wait(segbuf);
Ryusuke Konishid0676332010-03-22 19:33:43 +0900311 if (err && !ret)
312 ret = err;
Ryusuke Konishie29df392009-11-29 16:51:16 +0900313 }
Ryusuke Konishid0676332010-03-22 19:33:43 +0900314 return ret;
Ryusuke Konishie29df392009-11-29 16:51:16 +0900315}
316
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900317/**
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 */
322void 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
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700334/*
335 * BIO operations
336 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200337static void nilfs_end_bio_write(struct bio *bio)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700338{
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900339 struct nilfs_segment_buffer *segbuf = bio->bi_private;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700340
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200341 if (bio->bi_error)
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900342 atomic_inc(&segbuf->sb_err);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700343
344 bio_put(bio);
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900345 complete(&segbuf->sb_bio_event);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700346}
347
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900348static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer *segbuf,
Mike Christieb2d45862016-06-05 14:32:00 -0500349 struct nilfs_write_info *wi, int mode,
350 int mode_flags)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700351{
352 struct bio *bio = wi->bio;
353 int err;
354
Ryusuke Konishi026a7d62010-10-07 14:19:48 +0900355 if (segbuf->sb_nbio > 0 &&
356 bdi_write_congested(segbuf->sb_super->s_bdi)) {
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900357 wait_for_completion(&segbuf->sb_bio_event);
358 segbuf->sb_nbio--;
359 if (unlikely(atomic_read(&segbuf->sb_err))) {
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700360 bio_put(bio);
361 err = -EIO;
362 goto failed;
363 }
364 }
365
366 bio->bi_end_io = nilfs_end_bio_write;
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900367 bio->bi_private = segbuf;
Mike Christieb2d45862016-06-05 14:32:00 -0500368 bio_set_op_attrs(bio, mode, mode_flags);
Mike Christie4e49ea42016-06-05 14:31:41 -0500369 submit_bio(bio);
Vyacheslav Dubeyko4bf93b52013-08-22 16:35:45 -0700370 segbuf->sb_nbio++;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700371
372 wi->bio = NULL;
373 wi->rest_blocks -= wi->end - wi->start;
374 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
375 wi->start = wi->end;
376 return 0;
377
378 failed:
379 wi->bio = NULL;
380 return err;
381}
382
383/**
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900384 * nilfs_alloc_seg_bio - allocate a new bio for writing log
385 * @nilfs: nilfs object
386 * @start: start block number of the bio
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700387 * @nr_vecs: request size of page vector.
388 *
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700389 * Return Value: On success, pointer to the struct bio is returned.
390 * On error, NULL is returned.
391 */
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900392static struct bio *nilfs_alloc_seg_bio(struct the_nilfs *nilfs, sector_t start,
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700393 int nr_vecs)
394{
395 struct bio *bio;
396
Ryusuke Konishic1b353f2009-06-19 15:25:42 +0900397 bio = bio_alloc(GFP_NOIO, nr_vecs);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700398 if (bio == NULL) {
399 while (!bio && (nr_vecs >>= 1))
Ryusuke Konishic1b353f2009-06-19 15:25:42 +0900400 bio = bio_alloc(GFP_NOIO, nr_vecs);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700401 }
402 if (likely(bio)) {
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900403 bio->bi_bdev = nilfs->ns_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700404 bio->bi_iter.bi_sector =
405 start << (nilfs->ns_blocksize_bits - 9);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700406 }
407 return bio;
408}
409
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900410static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf,
411 struct nilfs_write_info *wi)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700412{
413 wi->bio = NULL;
414 wi->rest_blocks = segbuf->sb_sum.nblocks;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200415 wi->max_pages = BIO_MAX_PAGES;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700416 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
417 wi->start = wi->end = 0;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700418 wi->blocknr = segbuf->sb_pseg_start;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700419}
420
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900421static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer *segbuf,
422 struct nilfs_write_info *wi,
423 struct buffer_head *bh, int mode)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700424{
425 int len, err;
426
427 BUG_ON(wi->nr_vecs <= 0);
428 repeat:
429 if (!wi->bio) {
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900430 wi->bio = nilfs_alloc_seg_bio(wi->nilfs, wi->blocknr + wi->end,
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700431 wi->nr_vecs);
432 if (unlikely(!wi->bio))
433 return -ENOMEM;
434 }
435
436 len = bio_add_page(wi->bio, bh->b_page, bh->b_size, bh_offset(bh));
437 if (len == bh->b_size) {
438 wi->end++;
439 return 0;
440 }
441 /* bio is FULL */
Mike Christieb2d45862016-06-05 14:32:00 -0500442 err = nilfs_segbuf_submit_bio(segbuf, wi, mode, 0);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700443 /* never submit current bh */
444 if (likely(!err))
445 goto repeat;
446 return err;
447}
448
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900449/**
450 * nilfs_segbuf_write - submit write requests of a log
451 * @segbuf: buffer storing a log to be written
452 * @nilfs: nilfs object
453 *
454 * Return Value: On Success, 0 is returned. On Error, one of the following
455 * negative error code is returned.
456 *
457 * %-EIO - I/O error
458 *
459 * %-ENOMEM - Insufficient memory available.
460 */
Ryusuke Konishi6c477d42010-03-14 02:01:51 +0900461static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
462 struct the_nilfs *nilfs)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700463{
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900464 struct nilfs_write_info wi;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700465 struct buffer_head *bh;
Mike Christieb2d45862016-06-05 14:32:00 -0500466 int res = 0;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700467
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900468 wi.nilfs = nilfs;
469 nilfs_segbuf_prepare_write(segbuf, &wi);
470
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700471 list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) {
Mike Christieb2d45862016-06-05 14:32:00 -0500472 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, REQ_OP_WRITE);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700473 if (unlikely(res))
474 goto failed_bio;
475 }
476
477 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
Mike Christieb2d45862016-06-05 14:32:00 -0500478 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, REQ_OP_WRITE);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700479 if (unlikely(res))
480 goto failed_bio;
481 }
482
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900483 if (wi.bio) {
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700484 /*
485 * Last BIO is always sent through the following
486 * submission.
487 */
Mike Christieb2d45862016-06-05 14:32:00 -0500488 res = nilfs_segbuf_submit_bio(segbuf, &wi, REQ_OP_WRITE,
489 REQ_SYNC);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700490 }
491
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700492 failed_bio:
Ryusuke Konishi5f1586d2009-11-29 19:14:17 +0900493 return res;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700494}
495
496/**
497 * nilfs_segbuf_wait - wait for completion of requested BIOs
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900498 * @segbuf: segment buffer
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700499 *
500 * Return Value: On Success, 0 is returned. On Error, one of the following
501 * negative error code is returned.
502 *
503 * %-EIO - I/O error
504 */
Ryusuke Konishi6c477d42010-03-14 02:01:51 +0900505static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700506{
507 int err = 0;
508
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900509 if (!segbuf->sb_nbio)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700510 return 0;
511
512 do {
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900513 wait_for_completion(&segbuf->sb_bio_event);
514 } while (--segbuf->sb_nbio > 0);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700515
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900516 if (unlikely(atomic_read(&segbuf->sb_err) > 0)) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700517 nilfs_msg(segbuf->sb_super, KERN_ERR,
518 "I/O error writing log (start-blocknr=%llu, block-count=%lu) in segment %llu",
519 (unsigned long long)segbuf->sb_pseg_start,
520 segbuf->sb_sum.nblocks,
521 (unsigned long long)segbuf->sb_segnum);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700522 err = -EIO;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700523 }
524 return err;
525}