Linux 4.2.6
[linux/fpc-iii.git] / fs / nilfs2 / segbuf.c
blob42468e5ab3e71ab69d1f121323d6adb28cb67325
1 /*
2 * segbuf.c - NILFS segment buffer
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
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
20 * Written by Ryusuke Konishi <ryusuke@osrg.net>
24 #include <linux/buffer_head.h>
25 #include <linux/writeback.h>
26 #include <linux/crc32.h>
27 #include <linux/backing-dev.h>
28 #include <linux/slab.h>
29 #include "page.h"
30 #include "segbuf.h"
33 struct nilfs_write_info {
34 struct the_nilfs *nilfs;
35 struct bio *bio;
36 int start, end; /* The region to be submitted */
37 int rest_blocks;
38 int max_pages;
39 int nr_vecs;
40 sector_t blocknr;
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);
47 struct nilfs_segment_buffer *nilfs_segbuf_new(struct super_block *sb)
49 struct nilfs_segment_buffer *segbuf;
51 segbuf = kmem_cache_alloc(nilfs_segbuf_cachep, GFP_NOFS);
52 if (unlikely(!segbuf))
53 return NULL;
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);
59 segbuf->sb_super_root = NULL;
61 init_completion(&segbuf->sb_bio_event);
62 atomic_set(&segbuf->sb_err, 0);
63 segbuf->sb_nbio = 0;
65 return segbuf;
68 void nilfs_segbuf_free(struct nilfs_segment_buffer *segbuf)
70 kmem_cache_free(nilfs_segbuf_cachep, segbuf);
73 void nilfs_segbuf_map(struct nilfs_segment_buffer *segbuf, __u64 segnum,
74 unsigned long offset, struct the_nilfs *nilfs)
76 segbuf->sb_segnum = segnum;
77 nilfs_get_segment_range(nilfs, segnum, &segbuf->sb_fseg_start,
78 &segbuf->sb_fseg_end);
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;
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
90 void nilfs_segbuf_map_cont(struct nilfs_segment_buffer *segbuf,
91 struct nilfs_segment_buffer *prev)
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;
101 void nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer *segbuf,
102 __u64 nextnum, struct the_nilfs *nilfs)
104 segbuf->sb_nextnum = nextnum;
105 segbuf->sb_sum.next = nilfs_get_segment_start_blocknr(nilfs, nextnum);
108 int nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer *segbuf)
110 struct buffer_head *bh;
112 bh = sb_getblk(segbuf->sb_super,
113 segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk);
114 if (unlikely(!bh))
115 return -ENOMEM;
117 nilfs_segbuf_add_segsum_buffer(segbuf, bh);
118 return 0;
121 int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer *segbuf,
122 struct buffer_head **bhp)
124 struct buffer_head *bh;
126 bh = sb_getblk(segbuf->sb_super,
127 segbuf->sb_pseg_start + segbuf->sb_sum.nblocks);
128 if (unlikely(!bh))
129 return -ENOMEM;
131 nilfs_segbuf_add_payload_buffer(segbuf, bh);
132 *bhp = bh;
133 return 0;
136 int nilfs_segbuf_reset(struct nilfs_segment_buffer *segbuf, unsigned flags,
137 time_t ctime, __u64 cno)
139 int err;
141 segbuf->sb_sum.nblocks = segbuf->sb_sum.nsumblk = 0;
142 err = nilfs_segbuf_extend_segsum(segbuf);
143 if (unlikely(err))
144 return err;
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;
150 segbuf->sb_sum.cno = cno;
151 return 0;
155 * Setup segment summary
157 void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer *segbuf)
159 struct nilfs_segment_summary *raw_sum;
160 struct buffer_head *bh_sum;
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;
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;
176 raw_sum->ss_cno = cpu_to_le64(segbuf->sb_sum.cno);
180 * CRC calculation routines
182 static void
183 nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer *segbuf, u32 seed)
185 struct buffer_head *bh;
186 struct nilfs_segment_summary *raw_sum;
187 unsigned long size, bytes = segbuf->sb_sum.sumbytes;
188 u32 crc;
190 bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head,
191 b_assoc_buffers);
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)));
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);
207 raw_sum->ss_sumsum = cpu_to_le32(crc);
210 static void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer *segbuf,
211 u32 seed)
213 struct buffer_head *bh;
214 struct nilfs_segment_summary *raw_sum;
215 void *kaddr;
216 u32 crc;
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));
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);
229 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
230 kaddr = kmap_atomic(bh->b_page);
231 crc = crc32_le(crc, kaddr + bh_offset(bh), bh->b_size);
232 kunmap_atomic(kaddr);
234 raw_sum->ss_datasum = cpu_to_le32(crc);
237 static void
238 nilfs_segbuf_fill_in_super_root_crc(struct nilfs_segment_buffer *segbuf,
239 u32 seed)
241 struct nilfs_super_root *raw_sr;
242 struct the_nilfs *nilfs = segbuf->sb_super->s_fs_info;
243 unsigned srsize;
244 u32 crc;
246 raw_sr = (struct nilfs_super_root *)segbuf->sb_super_root->b_data;
247 srsize = NILFS_SR_BYTES(nilfs->ns_inode_size);
248 crc = crc32_le(seed,
249 (unsigned char *)raw_sr + sizeof(raw_sr->sr_sum),
250 srsize - sizeof(raw_sr->sr_sum));
251 raw_sr->sr_sum = cpu_to_le32(crc);
254 static void nilfs_release_buffers(struct list_head *list)
256 struct buffer_head *bh, *n;
258 list_for_each_entry_safe(bh, n, list, b_assoc_buffers) {
259 list_del_init(&bh->b_assoc_buffers);
260 brelse(bh);
264 static void nilfs_segbuf_clear(struct nilfs_segment_buffer *segbuf)
266 nilfs_release_buffers(&segbuf->sb_segsum_buffers);
267 nilfs_release_buffers(&segbuf->sb_payload_buffers);
268 segbuf->sb_super_root = NULL;
272 * Iterators for segment buffers
274 void nilfs_clear_logs(struct list_head *logs)
276 struct nilfs_segment_buffer *segbuf;
278 list_for_each_entry(segbuf, logs, sb_list)
279 nilfs_segbuf_clear(segbuf);
282 void nilfs_truncate_logs(struct list_head *logs,
283 struct nilfs_segment_buffer *last)
285 struct nilfs_segment_buffer *n, *segbuf;
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);
295 int nilfs_write_logs(struct list_head *logs, struct the_nilfs *nilfs)
297 struct nilfs_segment_buffer *segbuf;
298 int ret = 0;
300 list_for_each_entry(segbuf, logs, sb_list) {
301 ret = nilfs_segbuf_write(segbuf, nilfs);
302 if (ret)
303 break;
305 return ret;
308 int nilfs_wait_on_logs(struct list_head *logs)
310 struct nilfs_segment_buffer *segbuf;
311 int err, ret = 0;
313 list_for_each_entry(segbuf, logs, sb_list) {
314 err = nilfs_segbuf_wait(segbuf);
315 if (err && !ret)
316 ret = err;
318 return ret;
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
326 void nilfs_add_checksums_on_logs(struct list_head *logs, u32 seed)
328 struct nilfs_segment_buffer *segbuf;
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);
339 * BIO operations
341 static void nilfs_end_bio_write(struct bio *bio, int err)
343 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
344 struct nilfs_segment_buffer *segbuf = bio->bi_private;
346 if (!uptodate)
347 atomic_inc(&segbuf->sb_err);
349 bio_put(bio);
350 complete(&segbuf->sb_bio_event);
353 static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer *segbuf,
354 struct nilfs_write_info *wi, int mode)
356 struct bio *bio = wi->bio;
357 int err;
359 if (segbuf->sb_nbio > 0 &&
360 bdi_write_congested(segbuf->sb_super->s_bdi)) {
361 wait_for_completion(&segbuf->sb_bio_event);
362 segbuf->sb_nbio--;
363 if (unlikely(atomic_read(&segbuf->sb_err))) {
364 bio_put(bio);
365 err = -EIO;
366 goto failed;
370 bio->bi_end_io = nilfs_end_bio_write;
371 bio->bi_private = segbuf;
372 submit_bio(mode, bio);
373 segbuf->sb_nbio++;
375 wi->bio = NULL;
376 wi->rest_blocks -= wi->end - wi->start;
377 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
378 wi->start = wi->end;
379 return 0;
381 failed:
382 wi->bio = NULL;
383 return err;
387 * nilfs_alloc_seg_bio - allocate a new bio for writing log
388 * @nilfs: nilfs object
389 * @start: start block number of the bio
390 * @nr_vecs: request size of page vector.
392 * Return Value: On success, pointer to the struct bio is returned.
393 * On error, NULL is returned.
395 static struct bio *nilfs_alloc_seg_bio(struct the_nilfs *nilfs, sector_t start,
396 int nr_vecs)
398 struct bio *bio;
400 bio = bio_alloc(GFP_NOIO, nr_vecs);
401 if (bio == NULL) {
402 while (!bio && (nr_vecs >>= 1))
403 bio = bio_alloc(GFP_NOIO, nr_vecs);
405 if (likely(bio)) {
406 bio->bi_bdev = nilfs->ns_bdev;
407 bio->bi_iter.bi_sector =
408 start << (nilfs->ns_blocksize_bits - 9);
410 return bio;
413 static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf,
414 struct nilfs_write_info *wi)
416 wi->bio = NULL;
417 wi->rest_blocks = segbuf->sb_sum.nblocks;
418 wi->max_pages = bio_get_nr_vecs(wi->nilfs->ns_bdev);
419 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
420 wi->start = wi->end = 0;
421 wi->blocknr = segbuf->sb_pseg_start;
424 static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer *segbuf,
425 struct nilfs_write_info *wi,
426 struct buffer_head *bh, int mode)
428 int len, err;
430 BUG_ON(wi->nr_vecs <= 0);
431 repeat:
432 if (!wi->bio) {
433 wi->bio = nilfs_alloc_seg_bio(wi->nilfs, wi->blocknr + wi->end,
434 wi->nr_vecs);
435 if (unlikely(!wi->bio))
436 return -ENOMEM;
439 len = bio_add_page(wi->bio, bh->b_page, bh->b_size, bh_offset(bh));
440 if (len == bh->b_size) {
441 wi->end++;
442 return 0;
444 /* bio is FULL */
445 err = nilfs_segbuf_submit_bio(segbuf, wi, mode);
446 /* never submit current bh */
447 if (likely(!err))
448 goto repeat;
449 return err;
453 * nilfs_segbuf_write - submit write requests of a log
454 * @segbuf: buffer storing a log to be written
455 * @nilfs: nilfs object
457 * Return Value: On Success, 0 is returned. On Error, one of the following
458 * negative error code is returned.
460 * %-EIO - I/O error
462 * %-ENOMEM - Insufficient memory available.
464 static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
465 struct the_nilfs *nilfs)
467 struct nilfs_write_info wi;
468 struct buffer_head *bh;
469 int res = 0, rw = WRITE;
471 wi.nilfs = nilfs;
472 nilfs_segbuf_prepare_write(segbuf, &wi);
474 list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) {
475 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, rw);
476 if (unlikely(res))
477 goto failed_bio;
480 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
481 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, rw);
482 if (unlikely(res))
483 goto failed_bio;
486 if (wi.bio) {
488 * Last BIO is always sent through the following
489 * submission.
491 rw |= REQ_SYNC;
492 res = nilfs_segbuf_submit_bio(segbuf, &wi, rw);
495 failed_bio:
496 return res;
500 * nilfs_segbuf_wait - wait for completion of requested BIOs
501 * @segbuf: segment buffer
503 * Return Value: On Success, 0 is returned. On Error, one of the following
504 * negative error code is returned.
506 * %-EIO - I/O error
508 static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf)
510 int err = 0;
512 if (!segbuf->sb_nbio)
513 return 0;
515 do {
516 wait_for_completion(&segbuf->sb_bio_event);
517 } while (--segbuf->sb_nbio > 0);
519 if (unlikely(atomic_read(&segbuf->sb_err) > 0)) {
520 printk(KERN_ERR "NILFS: IO error writing segment\n");
521 err = -EIO;
523 return err;