fs: logfs: use bio_add_page() in __bdev_writeseg()
authorMing Lei <tom.leiming@gmail.com>
Fri, 11 Nov 2016 12:05:38 +0000 (20:05 +0800)
committerJens Axboe <axboe@fb.com>
Tue, 22 Nov 2016 15:57:55 +0000 (08:57 -0700)
Also this patch simplify the code a bit.

Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
Signed-off-by: Jens Axboe <axboe@fb.com>
fs/logfs/dev_bdev.c

index e01075790600033987e2ad68ab07d5164dc1441a..a2e8511b89d607353eeb9aebeb90bca860d68bcd 100644 (file)
@@ -71,56 +71,45 @@ static int __bdev_writeseg(struct super_block *sb, u64 ofs, pgoff_t index,
 {
        struct logfs_super *super = logfs_super(sb);
        struct address_space *mapping = super->s_mapping_inode->i_mapping;
-       struct bio *bio;
+       struct bio *bio = NULL;
        struct page *page;
        unsigned int max_pages;
-       int i;
+       int i, ret;
 
        max_pages = min_t(size_t, nr_pages, BIO_MAX_PAGES);
 
-       bio = bio_alloc(GFP_NOFS, max_pages);
-       BUG_ON(!bio);
-
        for (i = 0; i < nr_pages; i++) {
-               if (i >= max_pages) {
-                       /* Block layer cannot split bios :( */
-                       bio->bi_vcnt = i;
-                       bio->bi_iter.bi_size = i * PAGE_SIZE;
+               if (!bio) {
+                       bio = bio_alloc(GFP_NOFS, max_pages);
+                       BUG_ON(!bio);
+
                        bio->bi_bdev = super->s_bdev;
                        bio->bi_iter.bi_sector = ofs >> 9;
                        bio->bi_private = sb;
                        bio->bi_end_io = writeseg_end_io;
                        bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
-                       atomic_inc(&super->s_pending_writes);
-                       submit_bio(bio);
-
-                       ofs += i * PAGE_SIZE;
-                       index += i;
-                       nr_pages -= i;
-                       i = 0;
-
-                       bio = bio_alloc(GFP_NOFS, max_pages);
-                       BUG_ON(!bio);
                }
                page = find_lock_page(mapping, index + i);
                BUG_ON(!page);
-               bio->bi_io_vec[i].bv_page = page;
-               bio->bi_io_vec[i].bv_len = PAGE_SIZE;
-               bio->bi_io_vec[i].bv_offset = 0;
+               ret = bio_add_page(bio, page, PAGE_SIZE, 0);
 
                BUG_ON(PageWriteback(page));
                set_page_writeback(page);
                unlock_page(page);
+
+               if (!ret) {
+                       /* Block layer cannot split bios :( */
+                       ofs += bio->bi_iter.bi_size;
+                       atomic_inc(&super->s_pending_writes);
+                       submit_bio(bio);
+                       bio = NULL;
+               }
+       }
+
+       if (bio) {
+               atomic_inc(&super->s_pending_writes);
+               submit_bio(bio);
        }
-       bio->bi_vcnt = nr_pages;
-       bio->bi_iter.bi_size = nr_pages * PAGE_SIZE;
-       bio->bi_bdev = super->s_bdev;
-       bio->bi_iter.bi_sector = ofs >> 9;
-       bio->bi_private = sb;
-       bio->bi_end_io = writeseg_end_io;
-       bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
-       atomic_inc(&super->s_pending_writes);
-       submit_bio(bio);
        return 0;
 }