sbshc: acpi_device_class "smbus_host_controller" too long
[linux-2.6.git] / fs / nilfs2 / segbuf.c
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>
27 #include <linux/backing-dev.h>
28 #include <linux/slab.h>
29 #include "page.h"
30 #include "segbuf.h"
31
32
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;
41 };
42
43
44 static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
45                               struct the_nilfs *nilfs);
46 static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf);
47
48
49 static struct kmem_cache *nilfs_segbuf_cachep;
50
51 static void nilfs_segbuf_init_once(void *obj)
52 {
53         memset(obj, 0, sizeof(struct nilfs_segment_buffer));
54 }
55
56 int __init nilfs_init_segbuf_cache(void)
57 {
58         nilfs_segbuf_cachep =
59                 kmem_cache_create("nilfs2_segbuf_cache",
60                                   sizeof(struct nilfs_segment_buffer),
61                                   0, SLAB_RECLAIM_ACCOUNT,
62                                   nilfs_segbuf_init_once);
63
64         return (nilfs_segbuf_cachep == NULL) ? -ENOMEM : 0;
65 }
66
67 void nilfs_destroy_segbuf_cache(void)
68 {
69         kmem_cache_destroy(nilfs_segbuf_cachep);
70 }
71
72 struct nilfs_segment_buffer *nilfs_segbuf_new(struct super_block *sb)
73 {
74         struct nilfs_segment_buffer *segbuf;
75
76         segbuf = kmem_cache_alloc(nilfs_segbuf_cachep, GFP_NOFS);
77         if (unlikely(!segbuf))
78                 return NULL;
79
80         segbuf->sb_super = sb;
81         INIT_LIST_HEAD(&segbuf->sb_list);
82         INIT_LIST_HEAD(&segbuf->sb_segsum_buffers);
83         INIT_LIST_HEAD(&segbuf->sb_payload_buffers);
84
85         init_completion(&segbuf->sb_bio_event);
86         atomic_set(&segbuf->sb_err, 0);
87         segbuf->sb_nbio = 0;
88
89         return segbuf;
90 }
91
92 void nilfs_segbuf_free(struct nilfs_segment_buffer *segbuf)
93 {
94         kmem_cache_free(nilfs_segbuf_cachep, segbuf);
95 }
96
97 void nilfs_segbuf_map(struct nilfs_segment_buffer *segbuf, __u64 segnum,
98                      unsigned long offset, struct the_nilfs *nilfs)
99 {
100         segbuf->sb_segnum = segnum;
101         nilfs_get_segment_range(nilfs, segnum, &segbuf->sb_fseg_start,
102                                 &segbuf->sb_fseg_end);
103
104         segbuf->sb_pseg_start = segbuf->sb_fseg_start + offset;
105         segbuf->sb_rest_blocks =
106                 segbuf->sb_fseg_end - segbuf->sb_pseg_start + 1;
107 }
108
109 /**
110  * nilfs_segbuf_map_cont - map a new log behind a given log
111  * @segbuf: new segment buffer
112  * @prev: segment buffer containing a log to be continued
113  */
114 void nilfs_segbuf_map_cont(struct nilfs_segment_buffer *segbuf,
115                            struct nilfs_segment_buffer *prev)
116 {
117         segbuf->sb_segnum = prev->sb_segnum;
118         segbuf->sb_fseg_start = prev->sb_fseg_start;
119         segbuf->sb_fseg_end = prev->sb_fseg_end;
120         segbuf->sb_pseg_start = prev->sb_pseg_start + prev->sb_sum.nblocks;
121         segbuf->sb_rest_blocks =
122                 segbuf->sb_fseg_end - segbuf->sb_pseg_start + 1;
123 }
124
125 void nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer *segbuf,
126                                   __u64 nextnum, struct the_nilfs *nilfs)
127 {
128         segbuf->sb_nextnum = nextnum;
129         segbuf->sb_sum.next = nilfs_get_segment_start_blocknr(nilfs, nextnum);
130 }
131
132 int nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer *segbuf)
133 {
134         struct buffer_head *bh;
135
136         bh = sb_getblk(segbuf->sb_super,
137                        segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk);
138         if (unlikely(!bh))
139                 return -ENOMEM;
140
141         nilfs_segbuf_add_segsum_buffer(segbuf, bh);
142         return 0;
143 }
144
145 int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer *segbuf,
146                                 struct buffer_head **bhp)
147 {
148         struct buffer_head *bh;
149
150         bh = sb_getblk(segbuf->sb_super,
151                        segbuf->sb_pseg_start + segbuf->sb_sum.nblocks);
152         if (unlikely(!bh))
153                 return -ENOMEM;
154
155         nilfs_segbuf_add_payload_buffer(segbuf, bh);
156         *bhp = bh;
157         return 0;
158 }
159
160 int nilfs_segbuf_reset(struct nilfs_segment_buffer *segbuf, unsigned flags,
161                        time_t ctime)
162 {
163         int err;
164
165         segbuf->sb_sum.nblocks = segbuf->sb_sum.nsumblk = 0;
166         err = nilfs_segbuf_extend_segsum(segbuf);
167         if (unlikely(err))
168                 return err;
169
170         segbuf->sb_sum.flags = flags;
171         segbuf->sb_sum.sumbytes = sizeof(struct nilfs_segment_summary);
172         segbuf->sb_sum.nfinfo = segbuf->sb_sum.nfileblk = 0;
173         segbuf->sb_sum.ctime = ctime;
174         return 0;
175 }
176
177 /*
178  * Setup segment summary
179  */
180 void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer *segbuf)
181 {
182         struct nilfs_segment_summary *raw_sum;
183         struct buffer_head *bh_sum;
184
185         bh_sum = list_entry(segbuf->sb_segsum_buffers.next,
186                             struct buffer_head, b_assoc_buffers);
187         raw_sum = (struct nilfs_segment_summary *)bh_sum->b_data;
188
189         raw_sum->ss_magic    = cpu_to_le32(NILFS_SEGSUM_MAGIC);
190         raw_sum->ss_bytes    = cpu_to_le16(sizeof(*raw_sum));
191         raw_sum->ss_flags    = cpu_to_le16(segbuf->sb_sum.flags);
192         raw_sum->ss_seq      = cpu_to_le64(segbuf->sb_sum.seg_seq);
193         raw_sum->ss_create   = cpu_to_le64(segbuf->sb_sum.ctime);
194         raw_sum->ss_next     = cpu_to_le64(segbuf->sb_sum.next);
195         raw_sum->ss_nblocks  = cpu_to_le32(segbuf->sb_sum.nblocks);
196         raw_sum->ss_nfinfo   = cpu_to_le32(segbuf->sb_sum.nfinfo);
197         raw_sum->ss_sumbytes = cpu_to_le32(segbuf->sb_sum.sumbytes);
198         raw_sum->ss_pad      = 0;
199 }
200
201 /*
202  * CRC calculation routines
203  */
204 void nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer *segbuf,
205                                      u32 seed)
206 {
207         struct buffer_head *bh;
208         struct nilfs_segment_summary *raw_sum;
209         unsigned long size, bytes = segbuf->sb_sum.sumbytes;
210         u32 crc;
211
212         bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head,
213                         b_assoc_buffers);
214
215         raw_sum = (struct nilfs_segment_summary *)bh->b_data;
216         size = min_t(unsigned long, bytes, bh->b_size);
217         crc = crc32_le(seed,
218                        (unsigned char *)raw_sum +
219                        sizeof(raw_sum->ss_datasum) + sizeof(raw_sum->ss_sumsum),
220                        size - (sizeof(raw_sum->ss_datasum) +
221                                sizeof(raw_sum->ss_sumsum)));
222
223         list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers,
224                                      b_assoc_buffers) {
225                 bytes -= size;
226                 size = min_t(unsigned long, bytes, bh->b_size);
227                 crc = crc32_le(crc, bh->b_data, size);
228         }
229         raw_sum->ss_sumsum = cpu_to_le32(crc);
230 }
231
232 void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer *segbuf,
233                                    u32 seed)
234 {
235         struct buffer_head *bh;
236         struct nilfs_segment_summary *raw_sum;
237         void *kaddr;
238         u32 crc;
239
240         bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head,
241                         b_assoc_buffers);
242         raw_sum = (struct nilfs_segment_summary *)bh->b_data;
243         crc = crc32_le(seed,
244                        (unsigned char *)raw_sum + sizeof(raw_sum->ss_datasum),
245                        bh->b_size - sizeof(raw_sum->ss_datasum));
246
247         list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers,
248                                      b_assoc_buffers) {
249                 crc = crc32_le(crc, bh->b_data, bh->b_size);
250         }
251         list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
252                 kaddr = kmap_atomic(bh->b_page, KM_USER0);
253                 crc = crc32_le(crc, kaddr + bh_offset(bh), bh->b_size);
254                 kunmap_atomic(kaddr, KM_USER0);
255         }
256         raw_sum->ss_datasum = cpu_to_le32(crc);
257 }
258
259 static void nilfs_release_buffers(struct list_head *list)
260 {
261         struct buffer_head *bh, *n;
262
263         list_for_each_entry_safe(bh, n, list, b_assoc_buffers) {
264                 list_del_init(&bh->b_assoc_buffers);
265                 if (buffer_nilfs_allocated(bh)) {
266                         struct page *clone_page = bh->b_page;
267
268                         /* remove clone page */
269                         brelse(bh);
270                         page_cache_release(clone_page); /* for each bh */
271                         if (page_count(clone_page) <= 2) {
272                                 lock_page(clone_page);
273                                 nilfs_free_private_page(clone_page);
274                         }
275                         continue;
276                 }
277                 brelse(bh);
278         }
279 }
280
281 static void nilfs_segbuf_clear(struct nilfs_segment_buffer *segbuf)
282 {
283         nilfs_release_buffers(&segbuf->sb_segsum_buffers);
284         nilfs_release_buffers(&segbuf->sb_payload_buffers);
285 }
286
287 /*
288  * Iterators for segment buffers
289  */
290 void nilfs_clear_logs(struct list_head *logs)
291 {
292         struct nilfs_segment_buffer *segbuf;
293
294         list_for_each_entry(segbuf, logs, sb_list)
295                 nilfs_segbuf_clear(segbuf);
296 }
297
298 void nilfs_truncate_logs(struct list_head *logs,
299                          struct nilfs_segment_buffer *last)
300 {
301         struct nilfs_segment_buffer *n, *segbuf;
302
303         segbuf = list_prepare_entry(last, logs, sb_list);
304         list_for_each_entry_safe_continue(segbuf, n, logs, sb_list) {
305                 list_del_init(&segbuf->sb_list);
306                 nilfs_segbuf_clear(segbuf);
307                 nilfs_segbuf_free(segbuf);
308         }
309 }
310
311 int nilfs_write_logs(struct list_head *logs, struct the_nilfs *nilfs)
312 {
313         struct nilfs_segment_buffer *segbuf;
314         int ret = 0;
315
316         list_for_each_entry(segbuf, logs, sb_list) {
317                 ret = nilfs_segbuf_write(segbuf, nilfs);
318                 if (ret)
319                         break;
320         }
321         return ret;
322 }
323
324 int nilfs_wait_on_logs(struct list_head *logs)
325 {
326         struct nilfs_segment_buffer *segbuf;
327         int err, ret = 0;
328
329         list_for_each_entry(segbuf, logs, sb_list) {
330                 err = nilfs_segbuf_wait(segbuf);
331                 if (err && !ret)
332                         ret = err;
333         }
334         return ret;
335 }
336
337 /*
338  * BIO operations
339  */
340 static void nilfs_end_bio_write(struct bio *bio, int err)
341 {
342         const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
343         struct nilfs_segment_buffer *segbuf = bio->bi_private;
344
345         if (err == -EOPNOTSUPP) {
346                 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
347                 bio_put(bio);
348                 /* to be detected by submit_seg_bio() */
349         }
350
351         if (!uptodate)
352                 atomic_inc(&segbuf->sb_err);
353
354         bio_put(bio);
355         complete(&segbuf->sb_bio_event);
356 }
357
358 static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer *segbuf,
359                                    struct nilfs_write_info *wi, int mode)
360 {
361         struct bio *bio = wi->bio;
362         int err;
363
364         if (segbuf->sb_nbio > 0 && bdi_write_congested(wi->nilfs->ns_bdi)) {
365                 wait_for_completion(&segbuf->sb_bio_event);
366                 segbuf->sb_nbio--;
367                 if (unlikely(atomic_read(&segbuf->sb_err))) {
368                         bio_put(bio);
369                         err = -EIO;
370                         goto failed;
371                 }
372         }
373
374         bio->bi_end_io = nilfs_end_bio_write;
375         bio->bi_private = segbuf;
376         bio_get(bio);
377         submit_bio(mode, bio);
378         if (bio_flagged(bio, BIO_EOPNOTSUPP)) {
379                 bio_put(bio);
380                 err = -EOPNOTSUPP;
381                 goto failed;
382         }
383         segbuf->sb_nbio++;
384         bio_put(bio);
385
386         wi->bio = NULL;
387         wi->rest_blocks -= wi->end - wi->start;
388         wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
389         wi->start = wi->end;
390         return 0;
391
392  failed:
393         wi->bio = NULL;
394         return err;
395 }
396
397 /**
398  * nilfs_alloc_seg_bio - allocate a new bio for writing log
399  * @nilfs: nilfs object
400  * @start: start block number of the bio
401  * @nr_vecs: request size of page vector.
402  *
403  * Return Value: On success, pointer to the struct bio is returned.
404  * On error, NULL is returned.
405  */
406 static struct bio *nilfs_alloc_seg_bio(struct the_nilfs *nilfs, sector_t start,
407                                        int nr_vecs)
408 {
409         struct bio *bio;
410
411         bio = bio_alloc(GFP_NOIO, nr_vecs);
412         if (bio == NULL) {
413                 while (!bio && (nr_vecs >>= 1))
414                         bio = bio_alloc(GFP_NOIO, nr_vecs);
415         }
416         if (likely(bio)) {
417                 bio->bi_bdev = nilfs->ns_bdev;
418                 bio->bi_sector = start << (nilfs->ns_blocksize_bits - 9);
419         }
420         return bio;
421 }
422
423 static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf,
424                                        struct nilfs_write_info *wi)
425 {
426         wi->bio = NULL;
427         wi->rest_blocks = segbuf->sb_sum.nblocks;
428         wi->max_pages = bio_get_nr_vecs(wi->nilfs->ns_bdev);
429         wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
430         wi->start = wi->end = 0;
431         wi->blocknr = segbuf->sb_pseg_start;
432 }
433
434 static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer *segbuf,
435                                   struct nilfs_write_info *wi,
436                                   struct buffer_head *bh, int mode)
437 {
438         int len, err;
439
440         BUG_ON(wi->nr_vecs <= 0);
441  repeat:
442         if (!wi->bio) {
443                 wi->bio = nilfs_alloc_seg_bio(wi->nilfs, wi->blocknr + wi->end,
444                                               wi->nr_vecs);
445                 if (unlikely(!wi->bio))
446                         return -ENOMEM;
447         }
448
449         len = bio_add_page(wi->bio, bh->b_page, bh->b_size, bh_offset(bh));
450         if (len == bh->b_size) {
451                 wi->end++;
452                 return 0;
453         }
454         /* bio is FULL */
455         err = nilfs_segbuf_submit_bio(segbuf, wi, mode);
456         /* never submit current bh */
457         if (likely(!err))
458                 goto repeat;
459         return err;
460 }
461
462 /**
463  * nilfs_segbuf_write - submit write requests of a log
464  * @segbuf: buffer storing a log to be written
465  * @nilfs: nilfs object
466  *
467  * Return Value: On Success, 0 is returned. On Error, one of the following
468  * negative error code is returned.
469  *
470  * %-EIO - I/O error
471  *
472  * %-ENOMEM - Insufficient memory available.
473  */
474 static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
475                               struct the_nilfs *nilfs)
476 {
477         struct nilfs_write_info wi;
478         struct buffer_head *bh;
479         int res = 0, rw = WRITE;
480
481         wi.nilfs = nilfs;
482         nilfs_segbuf_prepare_write(segbuf, &wi);
483
484         list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) {
485                 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, rw);
486                 if (unlikely(res))
487                         goto failed_bio;
488         }
489
490         list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
491                 res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, rw);
492                 if (unlikely(res))
493                         goto failed_bio;
494         }
495
496         if (wi.bio) {
497                 /*
498                  * Last BIO is always sent through the following
499                  * submission.
500                  */
501                 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
502                 res = nilfs_segbuf_submit_bio(segbuf, &wi, rw);
503         }
504
505  failed_bio:
506         return res;
507 }
508
509 /**
510  * nilfs_segbuf_wait - wait for completion of requested BIOs
511  * @segbuf: segment buffer
512  *
513  * Return Value: On Success, 0 is returned. On Error, one of the following
514  * negative error code is returned.
515  *
516  * %-EIO - I/O error
517  */
518 static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf)
519 {
520         int err = 0;
521
522         if (!segbuf->sb_nbio)
523                 return 0;
524
525         do {
526                 wait_for_completion(&segbuf->sb_bio_event);
527         } while (--segbuf->sb_nbio > 0);
528
529         if (unlikely(atomic_read(&segbuf->sb_err) > 0)) {
530                 printk(KERN_ERR "NILFS: IO error writing segment\n");
531                 err = -EIO;
532         }
533         return err;
534 }