blob: f63620ce389223eca4464d472bc3eae4928bf910 [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 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Written by Ryusuke Konishi <ryusuke@osrg.net>
21 *
22 */
23
24#include <linux/buffer_head.h>
25#include <linux/writeback.h>
26#include <linux/crc32.h>
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +090027#include <linux/backing-dev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070029#include "page.h"
30#include "segbuf.h"
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070031
32
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +090033struct nilfs_write_info {
34 struct the_nilfs *nilfs;
35 struct bio *bio;
Ryusuke Konishi55480a02010-03-14 03:55:56 +090036 int start, end; /* The region to be submitted */
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +090037 int rest_blocks;
38 int max_pages;
39 int nr_vecs;
40 sector_t blocknr;
41};
42
Ryusuke Konishid1c6b722009-12-17 00:55:40 +090043static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
44 struct the_nilfs *nilfs);
45static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf);
46
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070047struct nilfs_segment_buffer *nilfs_segbuf_new(struct super_block *sb)
48{
49 struct nilfs_segment_buffer *segbuf;
50
51 segbuf = kmem_cache_alloc(nilfs_segbuf_cachep, GFP_NOFS);
52 if (unlikely(!segbuf))
53 return NULL;
54
55 segbuf->sb_super = sb;
56 INIT_LIST_HEAD(&segbuf->sb_list);
57 INIT_LIST_HEAD(&segbuf->sb_segsum_buffers);
58 INIT_LIST_HEAD(&segbuf->sb_payload_buffers);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +090059 segbuf->sb_super_root = NULL;
Ryusuke Konishi9284ad22009-11-25 01:04:21 +090060
61 init_completion(&segbuf->sb_bio_event);
62 atomic_set(&segbuf->sb_err, 0);
63 segbuf->sb_nbio = 0;
64
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070065 return segbuf;
66}
67
68void nilfs_segbuf_free(struct nilfs_segment_buffer *segbuf)
69{
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070070 kmem_cache_free(nilfs_segbuf_cachep, segbuf);
71}
72
Ryusuke Konishicece5522009-04-06 19:01:58 -070073void nilfs_segbuf_map(struct nilfs_segment_buffer *segbuf, __u64 segnum,
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070074 unsigned long offset, struct the_nilfs *nilfs)
75{
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070076 segbuf->sb_segnum = segnum;
77 nilfs_get_segment_range(nilfs, segnum, &segbuf->sb_fseg_start,
78 &segbuf->sb_fseg_end);
79
80 segbuf->sb_pseg_start = segbuf->sb_fseg_start + offset;
81 segbuf->sb_rest_blocks =
82 segbuf->sb_fseg_end - segbuf->sb_pseg_start + 1;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070083}
84
Ryusuke Konishia694291a2009-11-29 23:03:04 +090085/**
86 * nilfs_segbuf_map_cont - map a new log behind a given log
87 * @segbuf: new segment buffer
88 * @prev: segment buffer containing a log to be continued
89 */
90void nilfs_segbuf_map_cont(struct nilfs_segment_buffer *segbuf,
91 struct nilfs_segment_buffer *prev)
92{
93 segbuf->sb_segnum = prev->sb_segnum;
94 segbuf->sb_fseg_start = prev->sb_fseg_start;
95 segbuf->sb_fseg_end = prev->sb_fseg_end;
96 segbuf->sb_pseg_start = prev->sb_pseg_start + prev->sb_sum.nblocks;
97 segbuf->sb_rest_blocks =
98 segbuf->sb_fseg_end - segbuf->sb_pseg_start + 1;
99}
100
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700101void nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer *segbuf,
102 __u64 nextnum, struct the_nilfs *nilfs)
103{
104 segbuf->sb_nextnum = nextnum;
105 segbuf->sb_sum.next = nilfs_get_segment_start_blocknr(nilfs, nextnum);
106}
107
108int nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer *segbuf)
109{
110 struct buffer_head *bh;
111
112 bh = sb_getblk(segbuf->sb_super,
113 segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk);
114 if (unlikely(!bh))
115 return -ENOMEM;
116
117 nilfs_segbuf_add_segsum_buffer(segbuf, bh);
118 return 0;
119}
120
121int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer *segbuf,
122 struct buffer_head **bhp)
123{
124 struct buffer_head *bh;
125
126 bh = sb_getblk(segbuf->sb_super,
127 segbuf->sb_pseg_start + segbuf->sb_sum.nblocks);
128 if (unlikely(!bh))
129 return -ENOMEM;
130
131 nilfs_segbuf_add_payload_buffer(segbuf, bh);
132 *bhp = bh;
133 return 0;
134}
135
136int nilfs_segbuf_reset(struct nilfs_segment_buffer *segbuf, unsigned flags,
Ryusuke Konishi50614bc2010-04-10 17:59:15 +0900137 time_t ctime, __u64 cno)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700138{
139 int err;
140
141 segbuf->sb_sum.nblocks = segbuf->sb_sum.nsumblk = 0;
142 err = nilfs_segbuf_extend_segsum(segbuf);
143 if (unlikely(err))
144 return err;
145
146 segbuf->sb_sum.flags = flags;
147 segbuf->sb_sum.sumbytes = sizeof(struct nilfs_segment_summary);
148 segbuf->sb_sum.nfinfo = segbuf->sb_sum.nfileblk = 0;
149 segbuf->sb_sum.ctime = ctime;
Ryusuke Konishi50614bc2010-04-10 17:59:15 +0900150 segbuf->sb_sum.cno = cno;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700151 return 0;
152}
153
154/*
Ryusuke Konishi7a650042010-03-14 03:32:40 +0900155 * Setup segment summary
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700156 */
157void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer *segbuf)
158{
159 struct nilfs_segment_summary *raw_sum;
160 struct buffer_head *bh_sum;
161
162 bh_sum = list_entry(segbuf->sb_segsum_buffers.next,
163 struct buffer_head, b_assoc_buffers);
164 raw_sum = (struct nilfs_segment_summary *)bh_sum->b_data;
165
166 raw_sum->ss_magic = cpu_to_le32(NILFS_SEGSUM_MAGIC);
167 raw_sum->ss_bytes = cpu_to_le16(sizeof(*raw_sum));
168 raw_sum->ss_flags = cpu_to_le16(segbuf->sb_sum.flags);
169 raw_sum->ss_seq = cpu_to_le64(segbuf->sb_sum.seg_seq);
170 raw_sum->ss_create = cpu_to_le64(segbuf->sb_sum.ctime);
171 raw_sum->ss_next = cpu_to_le64(segbuf->sb_sum.next);
172 raw_sum->ss_nblocks = cpu_to_le32(segbuf->sb_sum.nblocks);
173 raw_sum->ss_nfinfo = cpu_to_le32(segbuf->sb_sum.nfinfo);
174 raw_sum->ss_sumbytes = cpu_to_le32(segbuf->sb_sum.sumbytes);
175 raw_sum->ss_pad = 0;
Ryusuke Konishi50614bc2010-04-10 17:59:15 +0900176 raw_sum->ss_cno = cpu_to_le64(segbuf->sb_sum.cno);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700177}
178
179/*
180 * CRC calculation routines
181 */
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900182static void
183nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer *segbuf, u32 seed)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700184{
185 struct buffer_head *bh;
186 struct nilfs_segment_summary *raw_sum;
187 unsigned long size, bytes = segbuf->sb_sum.sumbytes;
188 u32 crc;
189
190 bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head,
191 b_assoc_buffers);
192
193 raw_sum = (struct nilfs_segment_summary *)bh->b_data;
194 size = min_t(unsigned long, bytes, bh->b_size);
195 crc = crc32_le(seed,
196 (unsigned char *)raw_sum +
197 sizeof(raw_sum->ss_datasum) + sizeof(raw_sum->ss_sumsum),
198 size - (sizeof(raw_sum->ss_datasum) +
199 sizeof(raw_sum->ss_sumsum)));
200
201 list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers,
202 b_assoc_buffers) {
203 bytes -= size;
204 size = min_t(unsigned long, bytes, bh->b_size);
205 crc = crc32_le(crc, bh->b_data, size);
206 }
207 raw_sum->ss_sumsum = cpu_to_le32(crc);
208}
209
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900210static void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer *segbuf,
211 u32 seed)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700212{
213 struct buffer_head *bh;
214 struct nilfs_segment_summary *raw_sum;
215 void *kaddr;
216 u32 crc;
217
218 bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head,
219 b_assoc_buffers);
220 raw_sum = (struct nilfs_segment_summary *)bh->b_data;
221 crc = crc32_le(seed,
222 (unsigned char *)raw_sum + sizeof(raw_sum->ss_datasum),
223 bh->b_size - sizeof(raw_sum->ss_datasum));
224
225 list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers,
226 b_assoc_buffers) {
227 crc = crc32_le(crc, bh->b_data, bh->b_size);
228 }
229 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
Cong Wang7b9c0972011-11-25 23:14:33 +0800230 kaddr = kmap_atomic(bh->b_page);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700231 crc = crc32_le(crc, kaddr + bh_offset(bh), bh->b_size);
Cong Wang7b9c0972011-11-25 23:14:33 +0800232 kunmap_atomic(kaddr);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700233 }
234 raw_sum->ss_datasum = cpu_to_le32(crc);
235}
236
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900237static void
238nilfs_segbuf_fill_in_super_root_crc(struct nilfs_segment_buffer *segbuf,
239 u32 seed)
240{
241 struct nilfs_super_root *raw_sr;
Ryusuke Konishi6c6de1a2011-04-30 18:56:12 +0900242 struct the_nilfs *nilfs = segbuf->sb_super->s_fs_info;
243 unsigned srsize;
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900244 u32 crc;
245
246 raw_sr = (struct nilfs_super_root *)segbuf->sb_super_root->b_data;
Ryusuke Konishi6c6de1a2011-04-30 18:56:12 +0900247 srsize = NILFS_SR_BYTES(nilfs->ns_inode_size);
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900248 crc = crc32_le(seed,
249 (unsigned char *)raw_sr + sizeof(raw_sr->sr_sum),
Ryusuke Konishi6c6de1a2011-04-30 18:56:12 +0900250 srsize - sizeof(raw_sr->sr_sum));
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900251 raw_sr->sr_sum = cpu_to_le32(crc);
252}
253
Ryusuke Konishie29df392009-11-29 16:51:16 +0900254static void nilfs_release_buffers(struct list_head *list)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700255{
256 struct buffer_head *bh, *n;
257
258 list_for_each_entry_safe(bh, n, list, b_assoc_buffers) {
259 list_del_init(&bh->b_assoc_buffers);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700260 brelse(bh);
261 }
262}
263
Ryusuke Konishie29df392009-11-29 16:51:16 +0900264static void nilfs_segbuf_clear(struct nilfs_segment_buffer *segbuf)
265{
266 nilfs_release_buffers(&segbuf->sb_segsum_buffers);
267 nilfs_release_buffers(&segbuf->sb_payload_buffers);
Ryusuke Konishi1e2b68b2010-03-23 01:15:31 +0900268 segbuf->sb_super_root = NULL;
Ryusuke Konishie29df392009-11-29 16:51:16 +0900269}
270
271/*
272 * Iterators for segment buffers
273 */
274void nilfs_clear_logs(struct list_head *logs)
275{
276 struct nilfs_segment_buffer *segbuf;
277
278 list_for_each_entry(segbuf, logs, sb_list)
279 nilfs_segbuf_clear(segbuf);
280}
281
282void nilfs_truncate_logs(struct list_head *logs,
283 struct nilfs_segment_buffer *last)
284{
285 struct nilfs_segment_buffer *n, *segbuf;
286
287 segbuf = list_prepare_entry(last, logs, sb_list);
288 list_for_each_entry_safe_continue(segbuf, n, logs, sb_list) {
289 list_del_init(&segbuf->sb_list);
290 nilfs_segbuf_clear(segbuf);
291 nilfs_segbuf_free(segbuf);
292 }
293}
294
Ryusuke Konishid1c6b722009-12-17 00:55:40 +0900295int nilfs_write_logs(struct list_head *logs, struct the_nilfs *nilfs)
296{
297 struct nilfs_segment_buffer *segbuf;
298 int ret = 0;
299
300 list_for_each_entry(segbuf, logs, sb_list) {
301 ret = nilfs_segbuf_write(segbuf, nilfs);
302 if (ret)
303 break;
304 }
305 return ret;
306}
307
Ryusuke Konishie29df392009-11-29 16:51:16 +0900308int nilfs_wait_on_logs(struct list_head *logs)
309{
310 struct nilfs_segment_buffer *segbuf;
Ryusuke Konishid0676332010-03-22 19:33:43 +0900311 int err, ret = 0;
Ryusuke Konishie29df392009-11-29 16:51:16 +0900312
313 list_for_each_entry(segbuf, logs, sb_list) {
314 err = nilfs_segbuf_wait(segbuf);
Ryusuke Konishid0676332010-03-22 19:33:43 +0900315 if (err && !ret)
316 ret = err;
Ryusuke Konishie29df392009-11-29 16:51:16 +0900317 }
Ryusuke Konishid0676332010-03-22 19:33:43 +0900318 return ret;
Ryusuke Konishie29df392009-11-29 16:51:16 +0900319}
320
Ryusuke Konishiaaed1d52010-03-23 01:50:38 +0900321/**
322 * nilfs_add_checksums_on_logs - add checksums on the logs
323 * @logs: list of segment buffers storing target logs
324 * @seed: checksum seed value
325 */
326void nilfs_add_checksums_on_logs(struct list_head *logs, u32 seed)
327{
328 struct nilfs_segment_buffer *segbuf;
329
330 list_for_each_entry(segbuf, logs, sb_list) {
331 if (segbuf->sb_super_root)
332 nilfs_segbuf_fill_in_super_root_crc(segbuf, seed);
333 nilfs_segbuf_fill_in_segsum_crc(segbuf, seed);
334 nilfs_segbuf_fill_in_data_crc(segbuf, seed);
335 }
336}
337
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700338/*
339 * BIO operations
340 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200341static void nilfs_end_bio_write(struct bio *bio)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700342{
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900343 struct nilfs_segment_buffer *segbuf = bio->bi_private;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700344
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200345 if (bio->bi_error)
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900346 atomic_inc(&segbuf->sb_err);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700347
348 bio_put(bio);
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900349 complete(&segbuf->sb_bio_event);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700350}
351
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900352static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer *segbuf,
353 struct nilfs_write_info *wi, int mode)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700354{
355 struct bio *bio = wi->bio;
356 int err;
357
Ryusuke Konishi026a7d62010-10-07 14:19:48 +0900358 if (segbuf->sb_nbio > 0 &&
359 bdi_write_congested(segbuf->sb_super->s_bdi)) {
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900360 wait_for_completion(&segbuf->sb_bio_event);
361 segbuf->sb_nbio--;
362 if (unlikely(atomic_read(&segbuf->sb_err))) {
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700363 bio_put(bio);
364 err = -EIO;
365 goto failed;
366 }
367 }
368
369 bio->bi_end_io = nilfs_end_bio_write;
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900370 bio->bi_private = segbuf;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700371 submit_bio(mode, bio);
Vyacheslav Dubeyko4bf93b52013-08-22 16:35:45 -0700372 segbuf->sb_nbio++;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700373
374 wi->bio = NULL;
375 wi->rest_blocks -= wi->end - wi->start;
376 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
377 wi->start = wi->end;
378 return 0;
379
380 failed:
381 wi->bio = NULL;
382 return err;
383}
384
385/**
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900386 * nilfs_alloc_seg_bio - allocate a new bio for writing log
387 * @nilfs: nilfs object
388 * @start: start block number of the bio
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700389 * @nr_vecs: request size of page vector.
390 *
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700391 * Return Value: On success, pointer to the struct bio is returned.
392 * On error, NULL is returned.
393 */
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900394static struct bio *nilfs_alloc_seg_bio(struct the_nilfs *nilfs, sector_t start,
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700395 int nr_vecs)
396{
397 struct bio *bio;
398
Ryusuke Konishic1b353f2009-06-19 15:25:42 +0900399 bio = bio_alloc(GFP_NOIO, nr_vecs);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700400 if (bio == NULL) {
401 while (!bio && (nr_vecs >>= 1))
Ryusuke Konishic1b353f2009-06-19 15:25:42 +0900402 bio = bio_alloc(GFP_NOIO, nr_vecs);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700403 }
404 if (likely(bio)) {
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900405 bio->bi_bdev = nilfs->ns_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700406 bio->bi_iter.bi_sector =
407 start << (nilfs->ns_blocksize_bits - 9);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700408 }
409 return bio;
410}
411
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900412static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf,
413 struct nilfs_write_info *wi)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700414{
415 wi->bio = NULL;
416 wi->rest_blocks = segbuf->sb_sum.nblocks;
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200417 wi->max_pages = BIO_MAX_PAGES;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700418 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
419 wi->start = wi->end = 0;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700420 wi->blocknr = segbuf->sb_pseg_start;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700421}
422
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900423static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer *segbuf,
424 struct nilfs_write_info *wi,
425 struct buffer_head *bh, int mode)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700426{
427 int len, err;
428
429 BUG_ON(wi->nr_vecs <= 0);
430 repeat:
431 if (!wi->bio) {
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900432 wi->bio = nilfs_alloc_seg_bio(wi->nilfs, wi->blocknr + wi->end,
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700433 wi->nr_vecs);
434 if (unlikely(!wi->bio))
435 return -ENOMEM;
436 }
437
438 len = bio_add_page(wi->bio, bh->b_page, bh->b_size, bh_offset(bh));
439 if (len == bh->b_size) {
440 wi->end++;
441 return 0;
442 }
443 /* bio is FULL */
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900444 err = nilfs_segbuf_submit_bio(segbuf, wi, mode);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700445 /* never submit current bh */
446 if (likely(!err))
447 goto repeat;
448 return err;
449}
450
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900451/**
452 * nilfs_segbuf_write - submit write requests of a log
453 * @segbuf: buffer storing a log to be written
454 * @nilfs: nilfs object
455 *
456 * Return Value: On Success, 0 is returned. On Error, one of the following
457 * negative error code is returned.
458 *
459 * %-EIO - I/O error
460 *
461 * %-ENOMEM - Insufficient memory available.
462 */
Ryusuke Konishi6c477d42010-03-14 02:01:51 +0900463static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
464 struct the_nilfs *nilfs)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700465{
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900466 struct nilfs_write_info wi;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700467 struct buffer_head *bh;
Ryusuke Konishi5f1586d2009-11-29 19:14:17 +0900468 int res = 0, rw = WRITE;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700469
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900470 wi.nilfs = nilfs;
471 nilfs_segbuf_prepare_write(segbuf, &wi);
472
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700473 list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) {
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900474 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, rw);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700475 if (unlikely(res))
476 goto failed_bio;
477 }
478
479 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900480 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, rw);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700481 if (unlikely(res))
482 goto failed_bio;
483 }
484
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900485 if (wi.bio) {
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700486 /*
487 * Last BIO is always sent through the following
488 * submission.
489 */
Jens Axboe721a9602011-03-09 11:56:30 +0100490 rw |= REQ_SYNC;
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900491 res = nilfs_segbuf_submit_bio(segbuf, &wi, rw);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700492 }
493
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700494 failed_bio:
Ryusuke Konishi5f1586d2009-11-29 19:14:17 +0900495 return res;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700496}
497
498/**
499 * nilfs_segbuf_wait - wait for completion of requested BIOs
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900500 * @segbuf: segment buffer
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700501 *
502 * Return Value: On Success, 0 is returned. On Error, one of the following
503 * negative error code is returned.
504 *
505 * %-EIO - I/O error
506 */
Ryusuke Konishi6c477d42010-03-14 02:01:51 +0900507static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700508{
509 int err = 0;
510
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900511 if (!segbuf->sb_nbio)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700512 return 0;
513
514 do {
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900515 wait_for_completion(&segbuf->sb_bio_event);
516 } while (--segbuf->sb_nbio > 0);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700517
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900518 if (unlikely(atomic_read(&segbuf->sb_err) > 0)) {
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700519 printk(KERN_ERR "NILFS: IO error writing segment\n");
520 err = -EIO;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700521 }
522 return err;
523}