squashfs: migrate from ll_rw_block usage to BIO
[linux-2.6-microblaze.git] / fs / squashfs / block.c
index 4f9b9fb..64f6133 100644 (file)
@@ -13,6 +13,7 @@
  * datablocks and metadata blocks.
  */
 
+#include <linux/blkdev.h>
 #include <linux/fs.h>
 #include <linux/vfs.h>
 #include <linux/slab.h>
 #include "page_actor.h"
 
 /*
- * Read the metadata block length, this is stored in the first two
- * bytes of the metadata block.
+ * Returns the amount of bytes copied to the page actor.
  */
-static struct buffer_head *get_block_length(struct super_block *sb,
-                       u64 *cur_index, int *offset, int *length)
+static int copy_bio_to_actor(struct bio *bio,
+                            struct squashfs_page_actor *actor,
+                            int offset, int req_length)
+{
+       void *actor_addr = squashfs_first_page(actor);
+       struct bvec_iter_all iter_all = {};
+       struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
+       int copied_bytes = 0;
+       int actor_offset = 0;
+
+       if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all)))
+               return 0;
+
+       while (copied_bytes < req_length) {
+               int bytes_to_copy = min_t(int, bvec->bv_len - offset,
+                                         PAGE_SIZE - actor_offset);
+
+               bytes_to_copy = min_t(int, bytes_to_copy,
+                                     req_length - copied_bytes);
+               memcpy(actor_addr + actor_offset,
+                      page_address(bvec->bv_page) + bvec->bv_offset + offset,
+                      bytes_to_copy);
+
+               actor_offset += bytes_to_copy;
+               copied_bytes += bytes_to_copy;
+               offset += bytes_to_copy;
+
+               if (actor_offset >= PAGE_SIZE) {
+                       actor_addr = squashfs_next_page(actor);
+                       if (!actor_addr)
+                               break;
+                       actor_offset = 0;
+               }
+               if (offset >= bvec->bv_len) {
+                       if (!bio_next_segment(bio, &iter_all))
+                               break;
+                       offset = 0;
+               }
+       }
+       squashfs_finish_page(actor);
+       return copied_bytes;
+}
+
+static int squashfs_bio_read(struct super_block *sb, u64 index, int length,
+                            struct bio **biop, int *block_offset)
 {
        struct squashfs_sb_info *msblk = sb->s_fs_info;
-       struct buffer_head *bh;
-
-       bh = sb_bread(sb, *cur_index);
-       if (bh == NULL)
-               return NULL;
-
-       if (msblk->devblksize - *offset == 1) {
-               *length = (unsigned char) bh->b_data[*offset];
-               put_bh(bh);
-               bh = sb_bread(sb, ++(*cur_index));
-               if (bh == NULL)
-                       return NULL;
-               *length |= (unsigned char) bh->b_data[0] << 8;
-               *offset = 1;
-       } else {
-               *length = (unsigned char) bh->b_data[*offset] |
-                       (unsigned char) bh->b_data[*offset + 1] << 8;
-               *offset += 2;
-
-               if (*offset == msblk->devblksize) {
-                       put_bh(bh);
-                       bh = sb_bread(sb, ++(*cur_index));
-                       if (bh == NULL)
-                               return NULL;
-                       *offset = 0;
+       const u64 read_start = round_down(index, msblk->devblksize);
+       const sector_t block = read_start >> msblk->devblksize_log2;
+       const u64 read_end = round_up(index + length, msblk->devblksize);
+       const sector_t block_end = read_end >> msblk->devblksize_log2;
+       int offset = read_start - round_down(index, PAGE_SIZE);
+       int total_len = (block_end - block) << msblk->devblksize_log2;
+       const int page_count = DIV_ROUND_UP(total_len + offset, PAGE_SIZE);
+       int error, i;
+       struct bio *bio;
+
+       bio = bio_alloc(GFP_NOIO, page_count);
+       if (!bio)
+               return -ENOMEM;
+
+       bio_set_dev(bio, sb->s_bdev);
+       bio->bi_opf = READ;
+       bio->bi_iter.bi_sector = block * (msblk->devblksize >> SECTOR_SHIFT);
+
+       for (i = 0; i < page_count; ++i) {
+               unsigned int len =
+                       min_t(unsigned int, PAGE_SIZE - offset, total_len);
+               struct page *page = alloc_page(GFP_NOIO);
+
+               if (!page) {
+                       error = -ENOMEM;
+                       goto out_free_bio;
+               }
+               if (!bio_add_page(bio, page, len, offset)) {
+                       error = -EIO;
+                       goto out_free_bio;
                }
+               offset = 0;
+               total_len -= len;
        }
 
-       return bh;
-}
+       error = submit_bio_wait(bio);
+       if (error)
+               goto out_free_bio;
 
+       *biop = bio;
+       *block_offset = index & ((1 << msblk->devblksize_log2) - 1);
+       return 0;
+
+out_free_bio:
+       bio_free_pages(bio);
+       bio_put(bio);
+       return error;
+}
 
 /*
  * Read and decompress a metadata block or datablock.  Length is non-zero
@@ -76,129 +136,88 @@ static struct buffer_head *get_block_length(struct super_block *sb,
  * algorithms).
  */
 int squashfs_read_data(struct super_block *sb, u64 index, int length,
-               u64 *next_index, struct squashfs_page_actor *output)
+                      u64 *next_index, struct squashfs_page_actor *output)
 {
        struct squashfs_sb_info *msblk = sb->s_fs_info;
-       struct buffer_head **bh;
-       int offset = index & ((1 << msblk->devblksize_log2) - 1);
-       u64 cur_index = index >> msblk->devblksize_log2;
-       int bytes, compressed, b = 0, k = 0, avail, i;
-
-       bh = kcalloc(((output->length + msblk->devblksize - 1)
-               >> msblk->devblksize_log2) + 1, sizeof(*bh), GFP_KERNEL);
-       if (bh == NULL)
-               return -ENOMEM;
+       struct bio *bio = NULL;
+       int compressed;
+       int res;
+       int offset;
 
        if (length) {
                /*
                 * Datablock.
                 */
-               bytes = -offset;
                compressed = SQUASHFS_COMPRESSED_BLOCK(length);
                length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
-               if (next_index)
-                       *next_index = index + length;
-
                TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
                        index, compressed ? "" : "un", length, output->length);
-
-               if (length < 0 || length > output->length ||
-                               (index + length) > msblk->bytes_used)
-                       goto read_failure;
-
-               for (b = 0; bytes < length; b++, cur_index++) {
-                       bh[b] = sb_getblk(sb, cur_index);
-                       if (bh[b] == NULL)
-                               goto block_release;
-                       bytes += msblk->devblksize;
-               }
-               ll_rw_block(REQ_OP_READ, 0, b, bh);
        } else {
                /*
                 * Metadata block.
                 */
-               if ((index + 2) > msblk->bytes_used)
-                       goto read_failure;
+               const u8 *data;
+               struct bvec_iter_all iter_all = {};
+               struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
 
-               bh[0] = get_block_length(sb, &cur_index, &offset, &length);
-               if (bh[0] == NULL)
-                       goto read_failure;
-               b = 1;
+               if (index + 2 > msblk->bytes_used) {
+                       res = -EIO;
+                       goto out;
+               }
+               res = squashfs_bio_read(sb, index, 2, &bio, &offset);
+               if (res)
+                       goto out;
+
+               if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
+                       res = -EIO;
+                       goto out_free_bio;
+               }
+               /* Extract the length of the metadata block */
+               data = page_address(bvec->bv_page) + bvec->bv_offset;
+               length = data[offset];
+               if (offset <= bvec->bv_len - 1) {
+                       length |= data[offset + 1] << 8;
+               } else {
+                       if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
+                               res = -EIO;
+                               goto out_free_bio;
+                       }
+                       data = page_address(bvec->bv_page) + bvec->bv_offset;
+                       length |= data[0] << 8;
+               }
+               bio_free_pages(bio);
+               bio_put(bio);
 
-               bytes = msblk->devblksize - offset;
                compressed = SQUASHFS_COMPRESSED(length);
                length = SQUASHFS_COMPRESSED_SIZE(length);
-               if (next_index)
-                       *next_index = index + length + 2;
+               index += 2;
 
                TRACE("Block @ 0x%llx, %scompressed size %d\n", index,
-                               compressed ? "" : "un", length);
-
-               if (length < 0 || length > output->length ||
-                                       (index + length) > msblk->bytes_used)
-                       goto block_release;
-
-               for (; bytes < length; b++) {
-                       bh[b] = sb_getblk(sb, ++cur_index);
-                       if (bh[b] == NULL)
-                               goto block_release;
-                       bytes += msblk->devblksize;
-               }
-               ll_rw_block(REQ_OP_READ, 0, b - 1, bh + 1);
+                     compressed ? "" : "un", length);
        }
+       if (next_index)
+               *next_index = index + length;
 
-       for (i = 0; i < b; i++) {
-               wait_on_buffer(bh[i]);
-               if (!buffer_uptodate(bh[i]))
-                       goto block_release;
-       }
+       res = squashfs_bio_read(sb, index, length, &bio, &offset);
+       if (res)
+               goto out;
 
        if (compressed) {
-               if (!msblk->stream)
-                       goto read_failure;
-               length = squashfs_decompress(msblk, bh, b, offset, length,
-                       output);
-               if (length < 0)
-                       goto read_failure;
-       } else {
-               /*
-                * Block is uncompressed.
-                */
-               int in, pg_offset = 0;
-               void *data = squashfs_first_page(output);
-
-               for (bytes = length; k < b; k++) {
-                       in = min(bytes, msblk->devblksize - offset);
-                       bytes -= in;
-                       while (in) {
-                               if (pg_offset == PAGE_SIZE) {
-                                       data = squashfs_next_page(output);
-                                       pg_offset = 0;
-                               }
-                               avail = min_t(int, in, PAGE_SIZE -
-                                               pg_offset);
-                               memcpy(data + pg_offset, bh[k]->b_data + offset,
-                                               avail);
-                               in -= avail;
-                               pg_offset += avail;
-                               offset += avail;
-                       }
-                       offset = 0;
-                       put_bh(bh[k]);
+               if (!msblk->stream) {
+                       res = -EIO;
+                       goto out_free_bio;
                }
-               squashfs_finish_page(output);
+               res = squashfs_decompress(msblk, bio, offset, length, output);
+       } else {
+               res = copy_bio_to_actor(bio, output, offset, length);
        }
 
-       kfree(bh);
-       return length;
-
-block_release:
-       for (; k < b; k++)
-               put_bh(bh[k]);
+out_free_bio:
+       bio_free_pages(bio);
+       bio_put(bio);
+out:
+       if (res < 0)
+               ERROR("Failed to read block 0x%llx: %d\n", index, res);
 
-read_failure:
-       ERROR("squashfs_read_data failed to read block 0x%llx\n",
-                                       (unsigned long long) index);
-       kfree(bh);
-       return -EIO;
+       return res;
 }