blob: 636590c92c8bca949a8f09cb46d4534b437fb086 [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>
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070028#include "page.h"
29#include "segbuf.h"
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070030
31
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +090032struct nilfs_write_info {
33 struct the_nilfs *nilfs;
34 struct bio *bio;
35 int start, end; /* The region to be submitted */
36 int rest_blocks;
37 int max_pages;
38 int nr_vecs;
39 sector_t blocknr;
40};
41
42
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070043static struct kmem_cache *nilfs_segbuf_cachep;
44
45static void nilfs_segbuf_init_once(void *obj)
46{
47 memset(obj, 0, sizeof(struct nilfs_segment_buffer));
48}
49
50int __init nilfs_init_segbuf_cache(void)
51{
52 nilfs_segbuf_cachep =
53 kmem_cache_create("nilfs2_segbuf_cache",
54 sizeof(struct nilfs_segment_buffer),
55 0, SLAB_RECLAIM_ACCOUNT,
56 nilfs_segbuf_init_once);
57
58 return (nilfs_segbuf_cachep == NULL) ? -ENOMEM : 0;
59}
60
61void nilfs_destroy_segbuf_cache(void)
62{
63 kmem_cache_destroy(nilfs_segbuf_cachep);
64}
65
66struct nilfs_segment_buffer *nilfs_segbuf_new(struct super_block *sb)
67{
68 struct nilfs_segment_buffer *segbuf;
69
70 segbuf = kmem_cache_alloc(nilfs_segbuf_cachep, GFP_NOFS);
71 if (unlikely(!segbuf))
72 return NULL;
73
74 segbuf->sb_super = sb;
75 INIT_LIST_HEAD(&segbuf->sb_list);
76 INIT_LIST_HEAD(&segbuf->sb_segsum_buffers);
77 INIT_LIST_HEAD(&segbuf->sb_payload_buffers);
Ryusuke Konishi9284ad22009-11-25 01:04:21 +090078
79 init_completion(&segbuf->sb_bio_event);
80 atomic_set(&segbuf->sb_err, 0);
81 segbuf->sb_nbio = 0;
82
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070083 return segbuf;
84}
85
86void nilfs_segbuf_free(struct nilfs_segment_buffer *segbuf)
87{
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070088 kmem_cache_free(nilfs_segbuf_cachep, segbuf);
89}
90
Ryusuke Konishicece5522009-04-06 19:01:58 -070091void nilfs_segbuf_map(struct nilfs_segment_buffer *segbuf, __u64 segnum,
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070092 unsigned long offset, struct the_nilfs *nilfs)
93{
Ryusuke Konishi64b5a322009-04-06 19:01:36 -070094 segbuf->sb_segnum = segnum;
95 nilfs_get_segment_range(nilfs, segnum, &segbuf->sb_fseg_start,
96 &segbuf->sb_fseg_end);
97
98 segbuf->sb_pseg_start = segbuf->sb_fseg_start + offset;
99 segbuf->sb_rest_blocks =
100 segbuf->sb_fseg_end - segbuf->sb_pseg_start + 1;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700101}
102
103void nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer *segbuf,
104 __u64 nextnum, struct the_nilfs *nilfs)
105{
106 segbuf->sb_nextnum = nextnum;
107 segbuf->sb_sum.next = nilfs_get_segment_start_blocknr(nilfs, nextnum);
108}
109
110int nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer *segbuf)
111{
112 struct buffer_head *bh;
113
114 bh = sb_getblk(segbuf->sb_super,
115 segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk);
116 if (unlikely(!bh))
117 return -ENOMEM;
118
119 nilfs_segbuf_add_segsum_buffer(segbuf, bh);
120 return 0;
121}
122
123int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer *segbuf,
124 struct buffer_head **bhp)
125{
126 struct buffer_head *bh;
127
128 bh = sb_getblk(segbuf->sb_super,
129 segbuf->sb_pseg_start + segbuf->sb_sum.nblocks);
130 if (unlikely(!bh))
131 return -ENOMEM;
132
133 nilfs_segbuf_add_payload_buffer(segbuf, bh);
134 *bhp = bh;
135 return 0;
136}
137
138int nilfs_segbuf_reset(struct nilfs_segment_buffer *segbuf, unsigned flags,
139 time_t ctime)
140{
141 int err;
142
143 segbuf->sb_sum.nblocks = segbuf->sb_sum.nsumblk = 0;
144 err = nilfs_segbuf_extend_segsum(segbuf);
145 if (unlikely(err))
146 return err;
147
148 segbuf->sb_sum.flags = flags;
149 segbuf->sb_sum.sumbytes = sizeof(struct nilfs_segment_summary);
150 segbuf->sb_sum.nfinfo = segbuf->sb_sum.nfileblk = 0;
151 segbuf->sb_sum.ctime = ctime;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700152 return 0;
153}
154
155/*
156 * Setup segument summary
157 */
158void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer *segbuf)
159{
160 struct nilfs_segment_summary *raw_sum;
161 struct buffer_head *bh_sum;
162
163 bh_sum = list_entry(segbuf->sb_segsum_buffers.next,
164 struct buffer_head, b_assoc_buffers);
165 raw_sum = (struct nilfs_segment_summary *)bh_sum->b_data;
166
167 raw_sum->ss_magic = cpu_to_le32(NILFS_SEGSUM_MAGIC);
168 raw_sum->ss_bytes = cpu_to_le16(sizeof(*raw_sum));
169 raw_sum->ss_flags = cpu_to_le16(segbuf->sb_sum.flags);
170 raw_sum->ss_seq = cpu_to_le64(segbuf->sb_sum.seg_seq);
171 raw_sum->ss_create = cpu_to_le64(segbuf->sb_sum.ctime);
172 raw_sum->ss_next = cpu_to_le64(segbuf->sb_sum.next);
173 raw_sum->ss_nblocks = cpu_to_le32(segbuf->sb_sum.nblocks);
174 raw_sum->ss_nfinfo = cpu_to_le32(segbuf->sb_sum.nfinfo);
175 raw_sum->ss_sumbytes = cpu_to_le32(segbuf->sb_sum.sumbytes);
176 raw_sum->ss_pad = 0;
177}
178
179/*
180 * CRC calculation routines
181 */
182void nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer *segbuf,
183 u32 seed)
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
210void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer *segbuf,
211 u32 seed)
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) {
230 kaddr = kmap_atomic(bh->b_page, KM_USER0);
231 crc = crc32_le(crc, kaddr + bh_offset(bh), bh->b_size);
232 kunmap_atomic(kaddr, KM_USER0);
233 }
234 raw_sum->ss_datasum = cpu_to_le32(crc);
235}
236
237void nilfs_release_buffers(struct list_head *list)
238{
239 struct buffer_head *bh, *n;
240
241 list_for_each_entry_safe(bh, n, list, b_assoc_buffers) {
242 list_del_init(&bh->b_assoc_buffers);
243 if (buffer_nilfs_allocated(bh)) {
244 struct page *clone_page = bh->b_page;
245
246 /* remove clone page */
247 brelse(bh);
248 page_cache_release(clone_page); /* for each bh */
249 if (page_count(clone_page) <= 2) {
250 lock_page(clone_page);
251 nilfs_free_private_page(clone_page);
252 }
253 continue;
254 }
255 brelse(bh);
256 }
257}
258
259/*
260 * BIO operations
261 */
262static void nilfs_end_bio_write(struct bio *bio, int err)
263{
264 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900265 struct nilfs_segment_buffer *segbuf = bio->bi_private;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700266
267 if (err == -EOPNOTSUPP) {
268 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
269 bio_put(bio);
270 /* to be detected by submit_seg_bio() */
271 }
272
273 if (!uptodate)
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900274 atomic_inc(&segbuf->sb_err);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700275
276 bio_put(bio);
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900277 complete(&segbuf->sb_bio_event);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700278}
279
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900280static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer *segbuf,
281 struct nilfs_write_info *wi, int mode)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700282{
283 struct bio *bio = wi->bio;
284 int err;
285
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900286 if (segbuf->sb_nbio > 0 && bdi_write_congested(wi->nilfs->ns_bdi)) {
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900287 wait_for_completion(&segbuf->sb_bio_event);
288 segbuf->sb_nbio--;
289 if (unlikely(atomic_read(&segbuf->sb_err))) {
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700290 bio_put(bio);
291 err = -EIO;
292 goto failed;
293 }
294 }
295
296 bio->bi_end_io = nilfs_end_bio_write;
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900297 bio->bi_private = segbuf;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700298 bio_get(bio);
299 submit_bio(mode, bio);
300 if (bio_flagged(bio, BIO_EOPNOTSUPP)) {
301 bio_put(bio);
302 err = -EOPNOTSUPP;
303 goto failed;
304 }
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900305 segbuf->sb_nbio++;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700306 bio_put(bio);
307
308 wi->bio = NULL;
309 wi->rest_blocks -= wi->end - wi->start;
310 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
311 wi->start = wi->end;
312 return 0;
313
314 failed:
315 wi->bio = NULL;
316 return err;
317}
318
319/**
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900320 * nilfs_alloc_seg_bio - allocate a new bio for writing log
321 * @nilfs: nilfs object
322 * @start: start block number of the bio
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700323 * @nr_vecs: request size of page vector.
324 *
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700325 * Return Value: On success, pointer to the struct bio is returned.
326 * On error, NULL is returned.
327 */
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900328static struct bio *nilfs_alloc_seg_bio(struct the_nilfs *nilfs, sector_t start,
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700329 int nr_vecs)
330{
331 struct bio *bio;
332
Ryusuke Konishic1b353f2009-06-19 15:25:42 +0900333 bio = bio_alloc(GFP_NOIO, nr_vecs);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700334 if (bio == NULL) {
335 while (!bio && (nr_vecs >>= 1))
Ryusuke Konishic1b353f2009-06-19 15:25:42 +0900336 bio = bio_alloc(GFP_NOIO, nr_vecs);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700337 }
338 if (likely(bio)) {
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900339 bio->bi_bdev = nilfs->ns_bdev;
340 bio->bi_sector = start << (nilfs->ns_blocksize_bits - 9);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700341 }
342 return bio;
343}
344
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900345static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf,
346 struct nilfs_write_info *wi)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700347{
348 wi->bio = NULL;
349 wi->rest_blocks = segbuf->sb_sum.nblocks;
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900350 wi->max_pages = bio_get_nr_vecs(wi->nilfs->ns_bdev);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700351 wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
352 wi->start = wi->end = 0;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700353 wi->blocknr = segbuf->sb_pseg_start;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700354}
355
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900356static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer *segbuf,
357 struct nilfs_write_info *wi,
358 struct buffer_head *bh, int mode)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700359{
360 int len, err;
361
362 BUG_ON(wi->nr_vecs <= 0);
363 repeat:
364 if (!wi->bio) {
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900365 wi->bio = nilfs_alloc_seg_bio(wi->nilfs, wi->blocknr + wi->end,
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700366 wi->nr_vecs);
367 if (unlikely(!wi->bio))
368 return -ENOMEM;
369 }
370
371 len = bio_add_page(wi->bio, bh->b_page, bh->b_size, bh_offset(bh));
372 if (len == bh->b_size) {
373 wi->end++;
374 return 0;
375 }
376 /* bio is FULL */
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900377 err = nilfs_segbuf_submit_bio(segbuf, wi, mode);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700378 /* never submit current bh */
379 if (likely(!err))
380 goto repeat;
381 return err;
382}
383
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900384/**
385 * nilfs_segbuf_write - submit write requests of a log
386 * @segbuf: buffer storing a log to be written
387 * @nilfs: nilfs object
388 *
389 * Return Value: On Success, 0 is returned. On Error, one of the following
390 * negative error code is returned.
391 *
392 * %-EIO - I/O error
393 *
394 * %-ENOMEM - Insufficient memory available.
395 */
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700396int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900397 struct the_nilfs *nilfs)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700398{
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900399 struct nilfs_write_info wi;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700400 struct buffer_head *bh;
Ryusuke Konishi5f1586d2009-11-29 19:14:17 +0900401 int res = 0, rw = WRITE;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700402
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900403 wi.nilfs = nilfs;
404 nilfs_segbuf_prepare_write(segbuf, &wi);
405
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700406 list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) {
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900407 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, rw);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700408 if (unlikely(res))
409 goto failed_bio;
410 }
411
412 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900413 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, rw);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700414 if (unlikely(res))
415 goto failed_bio;
416 }
417
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900418 if (wi.bio) {
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700419 /*
420 * Last BIO is always sent through the following
421 * submission.
422 */
Ryusuke Konishi30bda0b2009-05-17 10:57:43 +0900423 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Ryusuke Konishi9c965ba2009-11-29 01:17:31 +0900424 res = nilfs_segbuf_submit_bio(segbuf, &wi, rw);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700425 }
426
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700427 failed_bio:
Ryusuke Konishi5f1586d2009-11-29 19:14:17 +0900428 return res;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700429}
430
431/**
432 * nilfs_segbuf_wait - wait for completion of requested BIOs
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900433 * @segbuf: segment buffer
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700434 *
435 * Return Value: On Success, 0 is returned. On Error, one of the following
436 * negative error code is returned.
437 *
438 * %-EIO - I/O error
439 */
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900440int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700441{
442 int err = 0;
443
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900444 if (!segbuf->sb_nbio)
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700445 return 0;
446
447 do {
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900448 wait_for_completion(&segbuf->sb_bio_event);
449 } while (--segbuf->sb_nbio > 0);
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700450
Ryusuke Konishi9284ad22009-11-25 01:04:21 +0900451 if (unlikely(atomic_read(&segbuf->sb_err) > 0)) {
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700452 printk(KERN_ERR "NILFS: IO error writing segment\n");
453 err = -EIO;
Ryusuke Konishi64b5a322009-04-06 19:01:36 -0700454 }
455 return err;
456}