Commit | Line | Data |
---|---|---|
64b5a32e RK |
1 | /* |
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> | |
9c965bac | 27 | #include <linux/backing-dev.h> |
5a0e3ad6 | 28 | #include <linux/slab.h> |
64b5a32e RK |
29 | #include "page.h" |
30 | #include "segbuf.h" | |
64b5a32e RK |
31 | |
32 | ||
9c965bac RK |
33 | struct nilfs_write_info { |
34 | struct the_nilfs *nilfs; | |
35 | struct bio *bio; | |
55480a06 | 36 | int start, end; /* The region to be submitted */ |
9c965bac RK |
37 | int rest_blocks; |
38 | int max_pages; | |
39 | int nr_vecs; | |
40 | sector_t blocknr; | |
41 | }; | |
42 | ||
d1c6b72a RK |
43 | static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf, |
44 | struct the_nilfs *nilfs); | |
45 | static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf); | |
46 | ||
64b5a32e RK |
47 | struct 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); | |
1e2b68bf | 59 | segbuf->sb_super_root = NULL; |
9284ad2a RK |
60 | |
61 | init_completion(&segbuf->sb_bio_event); | |
62 | atomic_set(&segbuf->sb_err, 0); | |
63 | segbuf->sb_nbio = 0; | |
64 | ||
64b5a32e RK |
65 | return segbuf; |
66 | } | |
67 | ||
68 | void nilfs_segbuf_free(struct nilfs_segment_buffer *segbuf) | |
69 | { | |
64b5a32e RK |
70 | kmem_cache_free(nilfs_segbuf_cachep, segbuf); |
71 | } | |
72 | ||
cece5520 | 73 | void nilfs_segbuf_map(struct nilfs_segment_buffer *segbuf, __u64 segnum, |
64b5a32e RK |
74 | unsigned long offset, struct the_nilfs *nilfs) |
75 | { | |
64b5a32e RK |
76 | 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; | |
64b5a32e RK |
83 | } |
84 | ||
a694291a RK |
85 | /** |
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 | */ | |
90 | void 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 | ||
64b5a32e RK |
101 | void 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 | ||
108 | int 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 | ||
121 | int 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 | ||
136 | int nilfs_segbuf_reset(struct nilfs_segment_buffer *segbuf, unsigned flags, | |
50614bcf | 137 | time_t ctime, __u64 cno) |
64b5a32e RK |
138 | { |
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; | |
50614bcf | 150 | segbuf->sb_sum.cno = cno; |
64b5a32e RK |
151 | return 0; |
152 | } | |
153 | ||
154 | /* | |
7a65004b | 155 | * Setup segment summary |
64b5a32e RK |
156 | */ |
157 | void 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; | |
50614bcf | 176 | raw_sum->ss_cno = cpu_to_le64(segbuf->sb_sum.cno); |
64b5a32e RK |
177 | } |
178 | ||
179 | /* | |
180 | * CRC calculation routines | |
181 | */ | |
aaed1d5b RK |
182 | static void |
183 | nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer *segbuf, u32 seed) | |
64b5a32e RK |
184 | { |
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 | ||
aaed1d5b RK |
210 | static void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer *segbuf, |
211 | u32 seed) | |
64b5a32e RK |
212 | { |
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) { | |
7b9c0976 | 230 | kaddr = kmap_atomic(bh->b_page); |
64b5a32e | 231 | crc = crc32_le(crc, kaddr + bh_offset(bh), bh->b_size); |
7b9c0976 | 232 | kunmap_atomic(kaddr); |
64b5a32e RK |
233 | } |
234 | raw_sum->ss_datasum = cpu_to_le32(crc); | |
235 | } | |
236 | ||
aaed1d5b RK |
237 | static void |
238 | nilfs_segbuf_fill_in_super_root_crc(struct nilfs_segment_buffer *segbuf, | |
239 | u32 seed) | |
240 | { | |
241 | struct nilfs_super_root *raw_sr; | |
6c6de1aa RK |
242 | struct the_nilfs *nilfs = segbuf->sb_super->s_fs_info; |
243 | unsigned srsize; | |
aaed1d5b RK |
244 | u32 crc; |
245 | ||
246 | raw_sr = (struct nilfs_super_root *)segbuf->sb_super_root->b_data; | |
6c6de1aa | 247 | srsize = NILFS_SR_BYTES(nilfs->ns_inode_size); |
aaed1d5b RK |
248 | crc = crc32_le(seed, |
249 | (unsigned char *)raw_sr + sizeof(raw_sr->sr_sum), | |
6c6de1aa | 250 | srsize - sizeof(raw_sr->sr_sum)); |
aaed1d5b RK |
251 | raw_sr->sr_sum = cpu_to_le32(crc); |
252 | } | |
253 | ||
e29df395 | 254 | static void nilfs_release_buffers(struct list_head *list) |
64b5a32e RK |
255 | { |
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); | |
64b5a32e RK |
260 | brelse(bh); |
261 | } | |
262 | } | |
263 | ||
e29df395 RK |
264 | static 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); | |
1e2b68bf | 268 | segbuf->sb_super_root = NULL; |
e29df395 RK |
269 | } |
270 | ||
271 | /* | |
272 | * Iterators for segment buffers | |
273 | */ | |
274 | void 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 | ||
282 | void 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 | ||
d1c6b72a RK |
295 | int 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 | ||
e29df395 RK |
308 | int nilfs_wait_on_logs(struct list_head *logs) |
309 | { | |
310 | struct nilfs_segment_buffer *segbuf; | |
d067633b | 311 | int err, ret = 0; |
e29df395 RK |
312 | |
313 | list_for_each_entry(segbuf, logs, sb_list) { | |
314 | err = nilfs_segbuf_wait(segbuf); | |
d067633b RK |
315 | if (err && !ret) |
316 | ret = err; | |
e29df395 | 317 | } |
d067633b | 318 | return ret; |
e29df395 RK |
319 | } |
320 | ||
aaed1d5b RK |
321 | /** |
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 | */ | |
326 | void 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 | ||
64b5a32e RK |
338 | /* |
339 | * BIO operations | |
340 | */ | |
341 | static void nilfs_end_bio_write(struct bio *bio, int err) | |
342 | { | |
343 | const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
9284ad2a | 344 | struct nilfs_segment_buffer *segbuf = bio->bi_private; |
64b5a32e RK |
345 | |
346 | if (err == -EOPNOTSUPP) { | |
347 | set_bit(BIO_EOPNOTSUPP, &bio->bi_flags); | |
b0e01ab2 | 348 | /* to be detected by nilfs_segbuf_submit_bio() */ |
64b5a32e RK |
349 | } |
350 | ||
351 | if (!uptodate) | |
9284ad2a | 352 | atomic_inc(&segbuf->sb_err); |
64b5a32e RK |
353 | |
354 | bio_put(bio); | |
9284ad2a | 355 | complete(&segbuf->sb_bio_event); |
64b5a32e RK |
356 | } |
357 | ||
9284ad2a RK |
358 | static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer *segbuf, |
359 | struct nilfs_write_info *wi, int mode) | |
64b5a32e RK |
360 | { |
361 | struct bio *bio = wi->bio; | |
362 | int err; | |
363 | ||
026a7d63 RK |
364 | if (segbuf->sb_nbio > 0 && |
365 | bdi_write_congested(segbuf->sb_super->s_bdi)) { | |
9284ad2a RK |
366 | wait_for_completion(&segbuf->sb_bio_event); |
367 | segbuf->sb_nbio--; | |
368 | if (unlikely(atomic_read(&segbuf->sb_err))) { | |
64b5a32e RK |
369 | bio_put(bio); |
370 | err = -EIO; | |
371 | goto failed; | |
372 | } | |
373 | } | |
374 | ||
375 | bio->bi_end_io = nilfs_end_bio_write; | |
9284ad2a | 376 | bio->bi_private = segbuf; |
64b5a32e RK |
377 | bio_get(bio); |
378 | submit_bio(mode, bio); | |
020269d2 | 379 | segbuf->sb_nbio++; |
64b5a32e RK |
380 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) { |
381 | bio_put(bio); | |
382 | err = -EOPNOTSUPP; | |
383 | goto failed; | |
384 | } | |
64b5a32e RK |
385 | bio_put(bio); |
386 | ||
387 | wi->bio = NULL; | |
388 | wi->rest_blocks -= wi->end - wi->start; | |
389 | wi->nr_vecs = min(wi->max_pages, wi->rest_blocks); | |
390 | wi->start = wi->end; | |
391 | return 0; | |
392 | ||
393 | failed: | |
394 | wi->bio = NULL; | |
395 | return err; | |
396 | } | |
397 | ||
398 | /** | |
9c965bac RK |
399 | * nilfs_alloc_seg_bio - allocate a new bio for writing log |
400 | * @nilfs: nilfs object | |
401 | * @start: start block number of the bio | |
64b5a32e RK |
402 | * @nr_vecs: request size of page vector. |
403 | * | |
64b5a32e RK |
404 | * Return Value: On success, pointer to the struct bio is returned. |
405 | * On error, NULL is returned. | |
406 | */ | |
9c965bac | 407 | static struct bio *nilfs_alloc_seg_bio(struct the_nilfs *nilfs, sector_t start, |
64b5a32e RK |
408 | int nr_vecs) |
409 | { | |
410 | struct bio *bio; | |
411 | ||
c1b353f0 | 412 | bio = bio_alloc(GFP_NOIO, nr_vecs); |
64b5a32e RK |
413 | if (bio == NULL) { |
414 | while (!bio && (nr_vecs >>= 1)) | |
c1b353f0 | 415 | bio = bio_alloc(GFP_NOIO, nr_vecs); |
64b5a32e RK |
416 | } |
417 | if (likely(bio)) { | |
9c965bac RK |
418 | bio->bi_bdev = nilfs->ns_bdev; |
419 | bio->bi_sector = start << (nilfs->ns_blocksize_bits - 9); | |
64b5a32e RK |
420 | } |
421 | return bio; | |
422 | } | |
423 | ||
9c965bac RK |
424 | static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf, |
425 | struct nilfs_write_info *wi) | |
64b5a32e RK |
426 | { |
427 | wi->bio = NULL; | |
428 | wi->rest_blocks = segbuf->sb_sum.nblocks; | |
9c965bac | 429 | wi->max_pages = bio_get_nr_vecs(wi->nilfs->ns_bdev); |
64b5a32e RK |
430 | wi->nr_vecs = min(wi->max_pages, wi->rest_blocks); |
431 | wi->start = wi->end = 0; | |
64b5a32e | 432 | wi->blocknr = segbuf->sb_pseg_start; |
64b5a32e RK |
433 | } |
434 | ||
9284ad2a RK |
435 | static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer *segbuf, |
436 | struct nilfs_write_info *wi, | |
437 | struct buffer_head *bh, int mode) | |
64b5a32e RK |
438 | { |
439 | int len, err; | |
440 | ||
441 | BUG_ON(wi->nr_vecs <= 0); | |
442 | repeat: | |
443 | if (!wi->bio) { | |
9c965bac | 444 | wi->bio = nilfs_alloc_seg_bio(wi->nilfs, wi->blocknr + wi->end, |
64b5a32e RK |
445 | wi->nr_vecs); |
446 | if (unlikely(!wi->bio)) | |
447 | return -ENOMEM; | |
448 | } | |
449 | ||
450 | len = bio_add_page(wi->bio, bh->b_page, bh->b_size, bh_offset(bh)); | |
451 | if (len == bh->b_size) { | |
452 | wi->end++; | |
453 | return 0; | |
454 | } | |
455 | /* bio is FULL */ | |
9284ad2a | 456 | err = nilfs_segbuf_submit_bio(segbuf, wi, mode); |
64b5a32e RK |
457 | /* never submit current bh */ |
458 | if (likely(!err)) | |
459 | goto repeat; | |
460 | return err; | |
461 | } | |
462 | ||
9c965bac RK |
463 | /** |
464 | * nilfs_segbuf_write - submit write requests of a log | |
465 | * @segbuf: buffer storing a log to be written | |
466 | * @nilfs: nilfs object | |
467 | * | |
468 | * Return Value: On Success, 0 is returned. On Error, one of the following | |
469 | * negative error code is returned. | |
470 | * | |
471 | * %-EIO - I/O error | |
472 | * | |
473 | * %-ENOMEM - Insufficient memory available. | |
474 | */ | |
6c477d44 RK |
475 | static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf, |
476 | struct the_nilfs *nilfs) | |
64b5a32e | 477 | { |
9c965bac | 478 | struct nilfs_write_info wi; |
64b5a32e | 479 | struct buffer_head *bh; |
5f1586d0 | 480 | int res = 0, rw = WRITE; |
64b5a32e | 481 | |
9c965bac RK |
482 | wi.nilfs = nilfs; |
483 | nilfs_segbuf_prepare_write(segbuf, &wi); | |
484 | ||
64b5a32e | 485 | list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) { |
9c965bac | 486 | res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, rw); |
64b5a32e RK |
487 | if (unlikely(res)) |
488 | goto failed_bio; | |
489 | } | |
490 | ||
491 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) { | |
9c965bac | 492 | res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, rw); |
64b5a32e RK |
493 | if (unlikely(res)) |
494 | goto failed_bio; | |
495 | } | |
496 | ||
9c965bac | 497 | if (wi.bio) { |
64b5a32e RK |
498 | /* |
499 | * Last BIO is always sent through the following | |
500 | * submission. | |
501 | */ | |
721a9602 | 502 | rw |= REQ_SYNC; |
9c965bac | 503 | res = nilfs_segbuf_submit_bio(segbuf, &wi, rw); |
64b5a32e RK |
504 | } |
505 | ||
64b5a32e | 506 | failed_bio: |
5f1586d0 | 507 | return res; |
64b5a32e RK |
508 | } |
509 | ||
510 | /** | |
511 | * nilfs_segbuf_wait - wait for completion of requested BIOs | |
9284ad2a | 512 | * @segbuf: segment buffer |
64b5a32e RK |
513 | * |
514 | * Return Value: On Success, 0 is returned. On Error, one of the following | |
515 | * negative error code is returned. | |
516 | * | |
517 | * %-EIO - I/O error | |
518 | */ | |
6c477d44 | 519 | static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf) |
64b5a32e RK |
520 | { |
521 | int err = 0; | |
522 | ||
9284ad2a | 523 | if (!segbuf->sb_nbio) |
64b5a32e RK |
524 | return 0; |
525 | ||
526 | do { | |
9284ad2a RK |
527 | wait_for_completion(&segbuf->sb_bio_event); |
528 | } while (--segbuf->sb_nbio > 0); | |
64b5a32e | 529 | |
9284ad2a | 530 | if (unlikely(atomic_read(&segbuf->sb_err) > 0)) { |
64b5a32e RK |
531 | printk(KERN_ERR "NILFS: IO error writing segment\n"); |
532 | err = -EIO; | |
64b5a32e RK |
533 | } |
534 | return err; | |
535 | } |