0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/blkdev.h>
0017 #include <linux/fs.h>
0018 #include <linux/vfs.h>
0019 #include <linux/slab.h>
0020 #include <linux/string.h>
0021 #include <linux/buffer_head.h>
0022 #include <linux/bio.h>
0023
0024 #include "squashfs_fs.h"
0025 #include "squashfs_fs_sb.h"
0026 #include "squashfs.h"
0027 #include "decompressor.h"
0028 #include "page_actor.h"
0029
0030
0031
0032
0033 static int copy_bio_to_actor(struct bio *bio,
0034 struct squashfs_page_actor *actor,
0035 int offset, int req_length)
0036 {
0037 void *actor_addr;
0038 struct bvec_iter_all iter_all = {};
0039 struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
0040 int copied_bytes = 0;
0041 int actor_offset = 0;
0042
0043 squashfs_actor_nobuff(actor);
0044 actor_addr = squashfs_first_page(actor);
0045
0046 if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all)))
0047 return 0;
0048
0049 while (copied_bytes < req_length) {
0050 int bytes_to_copy = min_t(int, bvec->bv_len - offset,
0051 PAGE_SIZE - actor_offset);
0052
0053 bytes_to_copy = min_t(int, bytes_to_copy,
0054 req_length - copied_bytes);
0055 if (!IS_ERR(actor_addr))
0056 memcpy(actor_addr + actor_offset, bvec_virt(bvec) +
0057 offset, bytes_to_copy);
0058
0059 actor_offset += bytes_to_copy;
0060 copied_bytes += bytes_to_copy;
0061 offset += bytes_to_copy;
0062
0063 if (actor_offset >= PAGE_SIZE) {
0064 actor_addr = squashfs_next_page(actor);
0065 if (!actor_addr)
0066 break;
0067 actor_offset = 0;
0068 }
0069 if (offset >= bvec->bv_len) {
0070 if (!bio_next_segment(bio, &iter_all))
0071 break;
0072 offset = 0;
0073 }
0074 }
0075 squashfs_finish_page(actor);
0076 return copied_bytes;
0077 }
0078
0079 static int squashfs_bio_read(struct super_block *sb, u64 index, int length,
0080 struct bio **biop, int *block_offset)
0081 {
0082 struct squashfs_sb_info *msblk = sb->s_fs_info;
0083 const u64 read_start = round_down(index, msblk->devblksize);
0084 const sector_t block = read_start >> msblk->devblksize_log2;
0085 const u64 read_end = round_up(index + length, msblk->devblksize);
0086 const sector_t block_end = read_end >> msblk->devblksize_log2;
0087 int offset = read_start - round_down(index, PAGE_SIZE);
0088 int total_len = (block_end - block) << msblk->devblksize_log2;
0089 const int page_count = DIV_ROUND_UP(total_len + offset, PAGE_SIZE);
0090 int error, i;
0091 struct bio *bio;
0092
0093 bio = bio_kmalloc(page_count, GFP_NOIO);
0094 if (!bio)
0095 return -ENOMEM;
0096 bio_init(bio, sb->s_bdev, bio->bi_inline_vecs, page_count, REQ_OP_READ);
0097 bio->bi_iter.bi_sector = block * (msblk->devblksize >> SECTOR_SHIFT);
0098
0099 for (i = 0; i < page_count; ++i) {
0100 unsigned int len =
0101 min_t(unsigned int, PAGE_SIZE - offset, total_len);
0102 struct page *page = alloc_page(GFP_NOIO);
0103
0104 if (!page) {
0105 error = -ENOMEM;
0106 goto out_free_bio;
0107 }
0108 if (!bio_add_page(bio, page, len, offset)) {
0109 error = -EIO;
0110 goto out_free_bio;
0111 }
0112 offset = 0;
0113 total_len -= len;
0114 }
0115
0116 error = submit_bio_wait(bio);
0117 if (error)
0118 goto out_free_bio;
0119
0120 *biop = bio;
0121 *block_offset = index & ((1 << msblk->devblksize_log2) - 1);
0122 return 0;
0123
0124 out_free_bio:
0125 bio_free_pages(bio);
0126 bio_uninit(bio);
0127 kfree(bio);
0128 return error;
0129 }
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140 int squashfs_read_data(struct super_block *sb, u64 index, int length,
0141 u64 *next_index, struct squashfs_page_actor *output)
0142 {
0143 struct squashfs_sb_info *msblk = sb->s_fs_info;
0144 struct bio *bio = NULL;
0145 int compressed;
0146 int res;
0147 int offset;
0148
0149 if (length) {
0150
0151
0152
0153 compressed = SQUASHFS_COMPRESSED_BLOCK(length);
0154 length = SQUASHFS_COMPRESSED_SIZE_BLOCK(length);
0155 TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
0156 index, compressed ? "" : "un", length, output->length);
0157 } else {
0158
0159
0160
0161 const u8 *data;
0162 struct bvec_iter_all iter_all = {};
0163 struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
0164
0165 if (index + 2 > msblk->bytes_used) {
0166 res = -EIO;
0167 goto out;
0168 }
0169 res = squashfs_bio_read(sb, index, 2, &bio, &offset);
0170 if (res)
0171 goto out;
0172
0173 if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
0174 res = -EIO;
0175 goto out_free_bio;
0176 }
0177
0178 data = bvec_virt(bvec);
0179 length = data[offset];
0180 if (offset < bvec->bv_len - 1) {
0181 length |= data[offset + 1] << 8;
0182 } else {
0183 if (WARN_ON_ONCE(!bio_next_segment(bio, &iter_all))) {
0184 res = -EIO;
0185 goto out_free_bio;
0186 }
0187 data = bvec_virt(bvec);
0188 length |= data[0] << 8;
0189 }
0190 bio_free_pages(bio);
0191 bio_uninit(bio);
0192 kfree(bio);
0193
0194 compressed = SQUASHFS_COMPRESSED(length);
0195 length = SQUASHFS_COMPRESSED_SIZE(length);
0196 index += 2;
0197
0198 TRACE("Block @ 0x%llx, %scompressed size %d\n", index - 2,
0199 compressed ? "" : "un", length);
0200 }
0201 if (length < 0 || length > output->length ||
0202 (index + length) > msblk->bytes_used) {
0203 res = -EIO;
0204 goto out;
0205 }
0206
0207 if (next_index)
0208 *next_index = index + length;
0209
0210 res = squashfs_bio_read(sb, index, length, &bio, &offset);
0211 if (res)
0212 goto out;
0213
0214 if (compressed) {
0215 if (!msblk->stream) {
0216 res = -EIO;
0217 goto out_free_bio;
0218 }
0219 res = squashfs_decompress(msblk, bio, offset, length, output);
0220 } else {
0221 res = copy_bio_to_actor(bio, output, offset, length);
0222 }
0223
0224 out_free_bio:
0225 bio_free_pages(bio);
0226 bio_uninit(bio);
0227 kfree(bio);
0228 out:
0229 if (res < 0) {
0230 ERROR("Failed to read block 0x%llx: %d\n", index, res);
0231 if (msblk->panic_on_errors)
0232 panic("squashfs read failed");
0233 }
0234
0235 return res;
0236 }