Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Interface to Linux block layer for MTD 'translation layers'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright © 2003-2010 David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/mtd/blktrans.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/blk-mq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/blkpg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/hdreg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "mtdcore.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static LIST_HEAD(blktrans_majors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static DEFINE_MUTEX(blktrans_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static void blktrans_dev_release(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct mtd_blktrans_dev *dev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		container_of(kref, struct mtd_blktrans_dev, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	dev->disk->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	blk_cleanup_queue(dev->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	blk_mq_free_tag_set(dev->tag_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	kfree(dev->tag_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	put_disk(dev->disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	list_del(&dev->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static struct mtd_blktrans_dev *blktrans_dev_get(struct gendisk *disk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct mtd_blktrans_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	mutex_lock(&blktrans_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	dev = disk->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	kref_get(&dev->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	mutex_unlock(&blktrans_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static void blktrans_dev_put(struct mtd_blktrans_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	mutex_lock(&blktrans_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	kref_put(&dev->ref, blktrans_dev_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	mutex_unlock(&blktrans_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static blk_status_t do_blktrans_request(struct mtd_blktrans_ops *tr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			       struct mtd_blktrans_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			       struct request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	unsigned long block, nsect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	block = blk_rq_pos(req) << 9 >> tr->blkshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	nsect = blk_rq_cur_bytes(req) >> tr->blkshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (req_op(req) == REQ_OP_FLUSH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		if (tr->flush(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	    get_capacity(req->rq_disk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	switch (req_op(req)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	case REQ_OP_DISCARD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		if (tr->discard(dev, block, nsect))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	case REQ_OP_READ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		buf = kmap(bio_page(req->bio)) + bio_offset(req->bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		for (; nsect > 0; nsect--, block++, buf += tr->blksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			if (tr->readsect(dev, block, buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 				kunmap(bio_page(req->bio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		kunmap(bio_page(req->bio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		rq_flush_dcache_pages(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	case REQ_OP_WRITE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		if (!tr->writesect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		rq_flush_dcache_pages(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		buf = kmap(bio_page(req->bio)) + bio_offset(req->bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		for (; nsect > 0; nsect--, block++, buf += tr->blksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			if (tr->writesect(dev, block, buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 				kunmap(bio_page(req->bio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		kunmap(bio_page(req->bio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int mtd_blktrans_cease_background(struct mtd_blktrans_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return dev->bg_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) EXPORT_SYMBOL_GPL(mtd_blktrans_cease_background);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static struct request *mtd_next_request(struct mtd_blktrans_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct request *rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	rq = list_first_entry_or_null(&dev->rq_list, struct request, queuelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (rq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		list_del_init(&rq->queuelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		blk_mq_start_request(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static void mtd_blktrans_work(struct mtd_blktrans_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	__releases(&dev->queue_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	__acquires(&dev->queue_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	struct mtd_blktrans_ops *tr = dev->tr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	struct request *req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int background_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		blk_status_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		dev->bg_stop = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		if (!req && !(req = mtd_next_request(dev))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			if (tr->background && !background_done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 				spin_unlock_irq(&dev->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 				mutex_lock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				tr->background(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 				mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				spin_lock_irq(&dev->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 				 * Do background processing just once per idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				 * period.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 				background_done = !dev->bg_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		spin_unlock_irq(&dev->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		mutex_lock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		res = do_blktrans_request(dev->tr, dev, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		if (!blk_update_request(req, res, blk_rq_cur_bytes(req))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			__blk_mq_end_request(req, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		background_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		spin_lock_irq(&dev->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static blk_status_t mtd_queue_rq(struct blk_mq_hw_ctx *hctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 				 const struct blk_mq_queue_data *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct mtd_blktrans_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	dev = hctx->queue->queuedata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		blk_mq_start_request(bd->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	spin_lock_irq(&dev->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	list_add_tail(&bd->rq->queuelist, &dev->rq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	mtd_blktrans_work(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	spin_unlock_irq(&dev->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int blktrans_open(struct block_device *bdev, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		return -ERESTARTSYS; /* FIXME: busy loop! -arnd*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	mutex_lock(&mtd_table_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	mutex_lock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (dev->open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	kref_get(&dev->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	__module_get(dev->tr->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (!dev->mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (dev->tr->open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		ret = dev->tr->open(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			goto error_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	ret = __get_mtd_device(dev->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		goto error_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	dev->file_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	dev->open++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	mutex_unlock(&mtd_table_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	blktrans_dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) error_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (dev->tr->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		dev->tr->release(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) error_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	module_put(dev->tr->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	kref_put(&dev->ref, blktrans_dev_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	mutex_unlock(&mtd_table_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	blktrans_dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static void blktrans_release(struct gendisk *disk, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct mtd_blktrans_dev *dev = blktrans_dev_get(disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	mutex_lock(&mtd_table_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	mutex_lock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (--dev->open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	kref_put(&dev->ref, blktrans_dev_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	module_put(dev->tr->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (dev->mtd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		if (dev->tr->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			dev->tr->release(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		__put_mtd_device(dev->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	mutex_unlock(&mtd_table_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	blktrans_dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	int ret = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	mutex_lock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (!dev->mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	ret = dev->tr->getgeo ? dev->tr->getgeo(dev, geo) : -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	blktrans_dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			      unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	int ret = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	mutex_lock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (!dev->mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	case BLKFLSBUF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		ret = dev->tr->flush ? dev->tr->flush(dev) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		ret = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	mutex_unlock(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	blktrans_dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static const struct block_device_operations mtd_block_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	.open		= blktrans_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	.release	= blktrans_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	.ioctl		= blktrans_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	.getgeo		= blktrans_getgeo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static const struct blk_mq_ops mtd_mq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	.queue_rq	= mtd_queue_rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	struct mtd_blktrans_ops *tr = new->tr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	struct mtd_blktrans_dev *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	int last_devnum = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	struct gendisk *gd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (mutex_trylock(&mtd_table_mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		mutex_unlock(&mtd_table_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	mutex_lock(&blktrans_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	list_for_each_entry(d, &tr->devs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		if (new->devnum == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			/* Use first free number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			if (d->devnum != last_devnum+1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				/* Found a free devnum. Plug it in here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 				new->devnum = last_devnum+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 				list_add_tail(&new->list, &d->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 				goto added;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		} else if (d->devnum == new->devnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			/* Required number taken */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			mutex_unlock(&blktrans_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		} else if (d->devnum > new->devnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			/* Required number was free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			list_add_tail(&new->list, &d->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			goto added;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		last_devnum = d->devnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	if (new->devnum == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		new->devnum = last_devnum+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	/* Check that the device and any partitions will get valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	 * minor numbers and that the disk naming code below can cope
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	 * with this number. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	if (new->devnum > (MINORMASK >> tr->part_bits) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	    (tr->part_bits && new->devnum >= 27 * 26)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		mutex_unlock(&blktrans_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		goto error1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	list_add_tail(&new->list, &tr->devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)  added:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	mutex_unlock(&blktrans_ref_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	mutex_init(&new->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	kref_init(&new->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	if (!tr->writesect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		new->readonly = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	/* Create gendisk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	gd = alloc_disk(1 << tr->part_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	if (!gd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	new->disk = gd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	gd->private_data = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	gd->major = tr->major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	gd->first_minor = (new->devnum) << tr->part_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	gd->fops = &mtd_block_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	if (tr->part_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		if (new->devnum < 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			snprintf(gd->disk_name, sizeof(gd->disk_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 				 "%s%c", tr->name, 'a' + new->devnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			snprintf(gd->disk_name, sizeof(gd->disk_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 				 "%s%c%c", tr->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 				 'a' - 1 + new->devnum / 26,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 				 'a' + new->devnum % 26);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		snprintf(gd->disk_name, sizeof(gd->disk_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			 "%s%d", tr->name, new->devnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	set_capacity(gd, ((u64)new->size * tr->blksize) >> 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	/* Create the request queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	spin_lock_init(&new->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	INIT_LIST_HEAD(&new->rq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	new->tag_set = kzalloc(sizeof(*new->tag_set), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	if (!new->tag_set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		goto error3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	new->rq = blk_mq_init_sq_queue(new->tag_set, &mtd_mq_ops, 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 				BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if (IS_ERR(new->rq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		ret = PTR_ERR(new->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		new->rq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		goto error4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	if (tr->flush)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		blk_queue_write_cache(new->rq, true, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	new->rq->queuedata = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	blk_queue_logical_block_size(new->rq, tr->blksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	blk_queue_flag_set(QUEUE_FLAG_NONROT, new->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, new->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if (tr->discard) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		blk_queue_flag_set(QUEUE_FLAG_DISCARD, new->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		blk_queue_max_discard_sectors(new->rq, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	gd->queue = new->rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	if (new->readonly)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		set_disk_ro(gd, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	device_add_disk(&new->mtd->dev, gd, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	if (new->disk_attributes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		ret = sysfs_create_group(&disk_to_dev(gd)->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 					new->disk_attributes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		WARN_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) error4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	kfree(new->tag_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) error3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	put_disk(new->disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) error2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	list_del(&new->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) error1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	if (mutex_trylock(&mtd_table_mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		mutex_unlock(&mtd_table_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (old->disk_attributes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 						old->disk_attributes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	/* Stop new requests to arrive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	del_gendisk(old->disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	/* Kill current requests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	spin_lock_irqsave(&old->queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	old->rq->queuedata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	spin_unlock_irqrestore(&old->queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	/* freeze+quiesce queue to ensure all requests are flushed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	blk_mq_freeze_queue(old->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	blk_mq_quiesce_queue(old->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	blk_mq_unquiesce_queue(old->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	blk_mq_unfreeze_queue(old->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	/* If the device is currently open, tell trans driver to close it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		then put mtd device, and don't touch it again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	mutex_lock(&old->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	if (old->open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		if (old->tr->release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			old->tr->release(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		__put_mtd_device(old->mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	old->mtd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	mutex_unlock(&old->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	blktrans_dev_put(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) static void blktrans_notify_remove(struct mtd_info *mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	struct mtd_blktrans_ops *tr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	struct mtd_blktrans_dev *dev, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	list_for_each_entry(tr, &blktrans_majors, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		list_for_each_entry_safe(dev, next, &tr->devs, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			if (dev->mtd == mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 				tr->remove_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static void blktrans_notify_add(struct mtd_info *mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	struct mtd_blktrans_ops *tr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	if (mtd->type == MTD_ABSENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	list_for_each_entry(tr, &blktrans_majors, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		tr->add_mtd(tr, mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) static struct mtd_notifier blktrans_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	.add = blktrans_notify_add,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	.remove = blktrans_notify_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	struct mtd_info *mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	/* Register the notifier if/when the first device type is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	   registered, to prevent the link/init ordering from fucking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	   us over. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	if (!blktrans_notifier.list.next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		register_mtd_user(&blktrans_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	mutex_lock(&mtd_table_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	ret = register_blkdev(tr->major, tr->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		       tr->name, tr->major, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		mutex_unlock(&mtd_table_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		tr->major = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	tr->blkshift = ffs(tr->blksize) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	INIT_LIST_HEAD(&tr->devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	list_add(&tr->list, &blktrans_majors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	mtd_for_each_device(mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		if (mtd->type != MTD_ABSENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 			tr->add_mtd(tr, mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	mutex_unlock(&mtd_table_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	struct mtd_blktrans_dev *dev, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	mutex_lock(&mtd_table_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	/* Remove it from the list of active majors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	list_del(&tr->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	list_for_each_entry_safe(dev, next, &tr->devs, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		tr->remove_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	unregister_blkdev(tr->major, tr->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	mutex_unlock(&mtd_table_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	BUG_ON(!list_empty(&tr->devs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) static void __exit mtd_blktrans_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	/* No race here -- if someone's currently in register_mtd_blktrans
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	   we're screwed anyway. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	if (blktrans_notifier.list.next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		unregister_mtd_user(&blktrans_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) module_exit(mtd_blktrans_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) EXPORT_SYMBOL_GPL(register_mtd_blktrans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");