/* * Copyright (c) 2019 Tom Marshall * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301, USA. */ #include #include #include #include #include #include #include #include #include #include /************************************** * Core memory management. **************************************/ struct page* cbd_alloc_page(void) { return alloc_page(GFP_KERNEL); } void cbd_free_page(struct page* page) { __free_page(page); } struct page* cbd_alloc_pages(size_t len) { return alloc_pages(GFP_KERNEL, get_order(len * PAGE_SIZE)); } void cbd_free_pages(struct page* pages, size_t len) { __free_pages(pages, get_order(len * PAGE_SIZE)); } bool cbd_alloc_pagev(struct page** pagev, size_t len) { size_t n; for (n = 0; n < len; ++n) { pagev[n] = cbd_alloc_page(); if (!pagev[n]) { goto err; } } return true; err: while (n--) { cbd_free_page(pagev[n]); pagev[n] = NULL; } return false; } void cbd_free_pagev(struct page** pagev, size_t len) { size_t n; for (n = 0; n < len; ++n) { cbd_free_page(pagev[n]); pagev[n] = NULL; } } /************************************** * Core low-level I/O. * * pblk count are in units of physical blocks (4096 bytes), NOT sectors. * data is a page address (obtained via __get_free_pages and friends). **************************************/ static struct bio* pblk_io_prepare(struct block_device* bdev, unsigned int op, u64 pblk, struct page* page) { struct bio* bio; bio = bio_alloc(GFP_KERNEL, 1); if (!bio) { printk(KERN_ERR "%s: out of memory\n", __func__); return NULL; } bio_set_dev(bio, bdev); bio->bi_opf = op; bio->bi_iter.bi_sector = pblk * (PBLK_SIZE / SECTOR_SIZE); if (bio_add_page(bio, page, PBLK_SIZE, 0) == 0) { BUG(); } return bio; } void pblk_endio(struct bio* bio) { struct compress_params* kparams = bio->bi_private; struct page* page = bio->bi_io_vec[0].bv_page; if (bio->bi_status != BLK_STS_OK) { printk(KERN_ERR "%s: I/O error\n", __func__); kparams->params.flags |= CBD_FLAG_ERROR; SetPageError(page); } ClearPageDirty(page); unlock_page(page); bio_put(bio); } int pblk_read_wait(struct compress_params* kparams, u64 pblk, struct page* page) { int ret; struct bio* bio; bio = pblk_io_prepare(kparams->dev, REQ_OP_READ, pblk, page); if (!bio) { printk(KERN_ERR "%s: out of memory\n", __func__); return -ENOMEM; } ret = submit_bio_wait(bio); if (ret) { printk(KERN_ERR "%s: submit_bio_wait failed: %d\n", __func__, ret); } bio_put(bio); return ret; } int pblk_readv_wait(struct compress_params* kparams, u64* pblkv, struct page** pagev, u32 count) { int ret = 0; u32 idx; struct bio* bio; /* XXX: Issue no-blocking reads for parallelism? */ for (idx = 0; idx < count; ++idx) { bio = pblk_io_prepare(kparams->dev, REQ_OP_READ, pblkv[idx], pagev[idx]); if (!bio) { printk(KERN_ERR "%s: out of memory\n", __func__); ret = -ENOMEM; goto iowait; } bio->bi_end_io = pblk_endio; bio->bi_private = kparams; submit_bio(bio); } iowait: while (idx > 0) { --idx; lock_page(pagev[idx]); if (PageError(pagev[idx])) { ret = -EIO; } } return ret; } int pblk_write_wait(struct compress_params* kparams, u64 pblk, struct page* page) { int ret; struct bio* bio; bio = pblk_io_prepare(kparams->dev, REQ_OP_WRITE, pblk, page); if (!bio) { printk(KERN_ERR "%s: out of memory\n", __func__); return -ENOMEM; } ret = submit_bio_wait(bio); if (ret) { printk(KERN_ERR "%s: submit_bio_wait failed: %d\n", __func__, ret); kparams->params.flags |= CBD_FLAG_ERROR; } bio_put(bio); return ret; } void pblk_write(struct compress_params* kparams, u64 pblk, struct page* page) { struct bio* bio; bio = pblk_io_prepare(kparams->dev, REQ_OP_WRITE, pblk, page); if (!bio) { printk(KERN_ERR "%s: out of memory\n", __func__); kparams->params.flags |= CBD_FLAG_ERROR; SetPageError(page); unlock_page(page); return; } bio->bi_end_io = pblk_endio; bio->bi_private = kparams; submit_bio(bio); } void pblk_writev(struct compress_params* kparams, u64* pblkv, struct page** pagev, u32 count) { u32 idx; for (idx = 0; idx < count; ++idx) { pblk_write(kparams, pblkv[idx], pagev[idx]); } }