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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2014 Ezequiel Garcia
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2011 Free Electrons
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Driver parameter handling strongly based on drivers/mtd/ubi/build.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *   Copyright (c) International Business Machines Corp., 2006
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *   Copyright (c) Nokia Corporation, 2007
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *   Authors: Artem Bityutskiy, Frank Haverkamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * Read-only block devices on top of UBI volumes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * A simple implementation to allow a block device to be layered on top of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * UBI volume. The implementation is provided by creating a static 1-to-1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * mapping between the block device and the UBI volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * The addressed byte is obtained from the addressed block sector, which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * mapped linearly into the corresponding LEB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *   LEB number = addressed byte / LEB size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * This feature is compiled in the UBI core, and adds a 'block' parameter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * to allow early creation of block devices on top of UBI volumes. Runtime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * block creation/removal for UBI volumes is provided through two UBI ioctls:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * UBI_IOCVOLCRBLK and UBI_IOCVOLRMBLK.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/mtd/ubi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <linux/blk-mq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include <linux/hdreg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #include <asm/div64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #include "ubi-media.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include "ubi.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) /* Maximum number of supported devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define UBIBLOCK_MAX_DEVICES 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) /* Maximum length of the 'block=' parameter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define UBIBLOCK_PARAM_LEN 63
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) /* Maximum number of comma-separated items in the 'block=' parameter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define UBIBLOCK_PARAM_COUNT 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) struct ubiblock_param {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	int ubi_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	int vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	char name[UBIBLOCK_PARAM_LEN+1];
^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) struct ubiblock_pdu {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct ubi_sgl usgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) /* Numbers of elements set in the @ubiblock_param array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static int ubiblock_devs __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) /* MTD devices specification parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES] __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) struct ubiblock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct ubi_volume_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	int ubi_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	int vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	int refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	int leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	struct gendisk *gd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct request_queue *rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct workqueue_struct *wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct mutex dev_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct blk_mq_tag_set tag_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) /* Linked list of all ubiblock instances */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static LIST_HEAD(ubiblock_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static DEFINE_IDR(ubiblock_minor_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) /* Protects ubiblock_devices and ubiblock_minor_idr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static DEFINE_MUTEX(devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static int ubiblock_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static int __init ubiblock_set_param(const char *val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 				     const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct ubiblock_param *param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	char buf[UBIBLOCK_PARAM_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	char *pbuf = &buf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	char *tokens[UBIBLOCK_PARAM_COUNT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (!val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	len = strnlen(val, UBIBLOCK_PARAM_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		pr_warn("UBI: block: empty 'block=' parameter - ignored\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (len == UBIBLOCK_PARAM_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		pr_err("UBI: block: parameter \"%s\" is too long, max. is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		       val, UBIBLOCK_PARAM_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	strcpy(buf, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	/* Get rid of the final newline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (buf[len - 1] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		buf[len - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	for (i = 0; i < UBIBLOCK_PARAM_COUNT; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		tokens[i] = strsep(&pbuf, ",");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	param = &ubiblock_param[ubiblock_devs];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (tokens[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		/* Two parameters: can be 'ubi, vol_id' or 'ubi, vol_name' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		ret = kstrtoint(tokens[0], 10, &param->ubi_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		/* Second param can be a number or a name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		ret = kstrtoint(tokens[1], 10, &param->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			param->vol_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			strcpy(param->name, tokens[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		/* One parameter: must be device path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		strcpy(param->name, tokens[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		param->ubi_num = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		param->vol_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	ubiblock_devs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static const struct kernel_param_ops ubiblock_param_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	.set    = ubiblock_set_param,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) module_param_cb(block, &ubiblock_param_ops, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) MODULE_PARM_DESC(block, "Attach block devices to UBI volumes. Parameter format: block=<path|dev,num|dev,name>.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			"Multiple \"block\" parameters may be specified.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			"UBI volumes may be specified by their number, name, or path to the device node.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			"Examples\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			"Using the UBI volume path:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			"ubi.block=/dev/ubi0_0\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			"Using the UBI device, and the volume name:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			"ubi.block=0,rootfs\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			"Using both UBI device number and UBI volume number:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			"ubi.block=0,0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static struct ubiblock *find_dev_nolock(int ubi_num, int vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	struct ubiblock *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	list_for_each_entry(dev, &ubiblock_devices, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		if (dev->ubi_num == ubi_num && dev->vol_id == vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return NULL;
^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) static int ubiblock_read(struct ubiblock_pdu *pdu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	int ret, leb, offset, bytes_left, to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	u64 pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct request *req = blk_mq_rq_from_pdu(pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	struct ubiblock *dev = req->q->queuedata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	to_read = blk_rq_bytes(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	pos = blk_rq_pos(req) << 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	/* Get LEB:offset address to read from */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	offset = do_div(pos, dev->leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	leb = pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	bytes_left = to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	while (bytes_left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		 * We can only read one LEB at a time. Therefore if the read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		 * length is larger than one LEB size, we split the operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		if (offset + to_read > dev->leb_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			to_read = dev->leb_size - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		ret = ubi_read_sg(dev->desc, leb, &pdu->usgl, offset, to_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		bytes_left -= to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		to_read = bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		leb += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static int ubiblock_open(struct block_device *bdev, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	struct ubiblock *dev = bdev->bd_disk->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	mutex_lock(&dev->dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (dev->refcnt > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		 * The volume is already open, just increase the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		 * counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		goto out_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	 * We want users to be aware they should only mount us as read-only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	 * It's just a paranoid check, as write requests will get rejected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	 * in any case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (mode & FMODE_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		ret = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	dev->desc = ubi_open_volume(dev->ubi_num, dev->vol_id, UBI_READONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (IS_ERR(dev->desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		dev_err(disk_to_dev(dev->gd), "failed to open ubi volume %d_%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			dev->ubi_num, dev->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		ret = PTR_ERR(dev->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		dev->desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) out_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	dev->refcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	mutex_unlock(&dev->dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	mutex_unlock(&dev->dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) static void ubiblock_release(struct gendisk *gd, fmode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	struct ubiblock *dev = gd->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	mutex_lock(&dev->dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	dev->refcnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (dev->refcnt == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		ubi_close_volume(dev->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		dev->desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	mutex_unlock(&dev->dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static int ubiblock_getgeo(struct block_device *bdev, struct hd_geometry *geo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	/* Some tools might require this information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	geo->heads = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	geo->cylinders = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	geo->sectors = get_capacity(bdev->bd_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	geo->start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static const struct block_device_operations ubiblock_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	.open = ubiblock_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	.release = ubiblock_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	.getgeo	= ubiblock_getgeo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static void ubiblock_do_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	struct ubiblock_pdu *pdu = container_of(work, struct ubiblock_pdu, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	struct request *req = blk_mq_rq_from_pdu(pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	blk_mq_start_request(req);
^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) 	 * It is safe to ignore the return value of blk_rq_map_sg() because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	 * the number of sg entries is limited to UBI_MAX_SG_COUNT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	 * and ubi_read_sg() will check that limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	blk_rq_map_sg(req->q, req, pdu->usgl.sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	ret = ubiblock_read(pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	rq_flush_dcache_pages(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	blk_mq_end_request(req, errno_to_blk_status(ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static blk_status_t ubiblock_queue_rq(struct blk_mq_hw_ctx *hctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			     const struct blk_mq_queue_data *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	struct request *req = bd->rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	struct ubiblock *dev = hctx->queue->queuedata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	struct ubiblock_pdu *pdu = blk_mq_rq_to_pdu(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	switch (req_op(req)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	case REQ_OP_READ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		ubi_sgl_init(&pdu->usgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		queue_work(dev->wq, &pdu->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		return BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		return BLK_STS_IOERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static int ubiblock_init_request(struct blk_mq_tag_set *set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		struct request *req, unsigned int hctx_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		unsigned int numa_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	struct ubiblock_pdu *pdu = blk_mq_rq_to_pdu(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	sg_init_table(pdu->usgl.sg, UBI_MAX_SG_COUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	INIT_WORK(&pdu->work, ubiblock_do_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) static const struct blk_mq_ops ubiblock_mq_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	.queue_rq       = ubiblock_queue_rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	.init_request	= ubiblock_init_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static int calc_disk_capacity(struct ubi_volume_info *vi, u64 *disk_capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	u64 size = vi->used_bytes >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (vi->used_bytes % 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		pr_warn("UBI: block: volume size is not a multiple of 512, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			"last %llu bytes are ignored!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			vi->used_bytes - (size << 9));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if ((sector_t)size != size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	*disk_capacity = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) int ubiblock_create(struct ubi_volume_info *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	struct ubiblock *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	struct gendisk *gd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	u64 disk_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	ret = calc_disk_capacity(vi, &disk_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	/* Check that the volume isn't already handled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (find_dev_nolock(vi->ubi_num, vi->vol_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	dev = kzalloc(sizeof(struct ubiblock), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	mutex_init(&dev->dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	dev->ubi_num = vi->ubi_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	dev->vol_id = vi->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	dev->leb_size = vi->usable_leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	/* Initialize the gendisk of this ubiblock device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	gd = alloc_disk(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (!gd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		pr_err("UBI: block: alloc_disk failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		goto out_free_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	gd->fops = &ubiblock_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	gd->major = ubiblock_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	gd->first_minor = idr_alloc(&ubiblock_minor_idr, dev, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (gd->first_minor < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		dev_err(disk_to_dev(gd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			"block: dynamic minor allocation failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		goto out_put_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	gd->private_data = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	sprintf(gd->disk_name, "ubiblock%d_%d", dev->ubi_num, dev->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	set_capacity(gd, disk_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	dev->gd = gd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	dev->tag_set.ops = &ubiblock_mq_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	dev->tag_set.queue_depth = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	dev->tag_set.numa_node = NUMA_NO_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	dev->tag_set.cmd_size = sizeof(struct ubiblock_pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	dev->tag_set.driver_data = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	dev->tag_set.nr_hw_queues = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	ret = blk_mq_alloc_tag_set(&dev->tag_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		dev_err(disk_to_dev(dev->gd), "blk_mq_alloc_tag_set failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		goto out_remove_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	dev->rq = blk_mq_init_queue(&dev->tag_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if (IS_ERR(dev->rq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		dev_err(disk_to_dev(gd), "blk_mq_init_queue failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		ret = PTR_ERR(dev->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		goto out_free_tags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	blk_queue_max_segments(dev->rq, UBI_MAX_SG_COUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	dev->rq->queuedata = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	dev->gd->queue = dev->rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	 * Create one workqueue per volume (per registered block device).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	 * Rembember workqueues are cheap, they're not threads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	dev->wq = alloc_workqueue("%s", 0, 0, gd->disk_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	if (!dev->wq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		goto out_free_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	list_add_tail(&dev->list, &ubiblock_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	/* Must be the last step: anyone can call file ops from now on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	add_disk(dev->gd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	dev_info(disk_to_dev(dev->gd), "created from ubi%d:%d(%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		 dev->ubi_num, dev->vol_id, vi->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) out_free_queue:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	blk_cleanup_queue(dev->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) out_free_tags:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	blk_mq_free_tag_set(&dev->tag_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) out_remove_minor:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	idr_remove(&ubiblock_minor_idr, gd->first_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) out_put_disk:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	put_disk(dev->gd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) out_free_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) static void ubiblock_cleanup(struct ubiblock *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	/* Stop new requests to arrive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	del_gendisk(dev->gd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	/* Flush pending work */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	destroy_workqueue(dev->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	/* Finally destroy the blk queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	blk_cleanup_queue(dev->rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	blk_mq_free_tag_set(&dev->tag_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	dev_info(disk_to_dev(dev->gd), "released");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	idr_remove(&ubiblock_minor_idr, dev->gd->first_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	put_disk(dev->gd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) int ubiblock_remove(struct ubi_volume_info *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	struct ubiblock *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	/* Found a device, let's lock it so we can check if it's busy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	mutex_lock(&dev->dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	if (dev->refcnt > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		goto out_unlock_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	/* Remove from device list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	list_del(&dev->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	ubiblock_cleanup(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	mutex_unlock(&dev->dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) out_unlock_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	mutex_unlock(&dev->dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) static int ubiblock_resize(struct ubi_volume_info *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	struct ubiblock *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	u64 disk_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	 * Need to lock the device list until we stop using the device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	 * otherwise the device struct might get released in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	 * 'ubiblock_remove()'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	ret = calc_disk_capacity(vi, &disk_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		if (ret == -EFBIG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 			dev_warn(disk_to_dev(dev->gd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 				 "the volume is too big (%d LEBs), cannot resize",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 				 vi->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	mutex_lock(&dev->dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	if (get_capacity(dev->gd) != disk_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		set_capacity(dev->gd, disk_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		dev_info(disk_to_dev(dev->gd), "resized to %lld bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 			 vi->used_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	mutex_unlock(&dev->dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) static int ubiblock_notify(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 			 unsigned long notification_type, void *ns_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	struct ubi_notification *nt = ns_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	switch (notification_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	case UBI_VOLUME_ADDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		 * We want to enforce explicit block device creation for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		 * volumes, so when a volume is added we do nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	case UBI_VOLUME_REMOVED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		ubiblock_remove(&nt->vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	case UBI_VOLUME_RESIZED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		ubiblock_resize(&nt->vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	case UBI_VOLUME_UPDATED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		 * If the volume is static, a content update might mean the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		 * size (i.e. used_bytes) was also changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		if (nt->vi.vol_type == UBI_STATIC_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 			ubiblock_resize(&nt->vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) static struct notifier_block ubiblock_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	.notifier_call = ubiblock_notify,
^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 struct ubi_volume_desc * __init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) open_volume_desc(const char *name, int ubi_num, int vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	if (ubi_num == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		/* No ubi num, name must be a vol device path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		return ubi_open_volume_path(name, UBI_READONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	else if (vol_id == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		/* No vol_id, must be vol_name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		return ubi_open_volume_nm(ubi_num, name, UBI_READONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		return ubi_open_volume(ubi_num, vol_id, UBI_READONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) static void __init ubiblock_create_from_param(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	int i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	struct ubiblock_param *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	struct ubi_volume_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	struct ubi_volume_info vi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	 * If there is an error creating one of the ubiblocks, continue on to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	 * create the following ubiblocks. This helps in a circumstance where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	 * the kernel command-line specifies multiple block devices and some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	 * may be broken, but we still want the working ones to come up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	for (i = 0; i < ubiblock_devs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		p = &ubiblock_param[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		desc = open_volume_desc(p->name, p->ubi_num, p->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		if (IS_ERR(desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 			pr_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 			       "UBI: block: can't open volume on ubi%d_%d, err=%ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 			       p->ubi_num, p->vol_id, PTR_ERR(desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		ubi_get_volume_info(desc, &vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		ubi_close_volume(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		ret = ubiblock_create(&vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 			pr_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 			       "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 			       vi.name, p->ubi_num, p->vol_id, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) static void ubiblock_remove_all(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	struct ubiblock *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	struct ubiblock *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	list_for_each_entry_safe(dev, next, &ubiblock_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		/* The module is being forcefully removed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		WARN_ON(dev->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		/* Remove from device list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		list_del(&dev->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 		ubiblock_cleanup(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) int __init ubiblock_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	ubiblock_major = register_blkdev(0, "ubiblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	if (ubiblock_major < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		return ubiblock_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	 * Attach block devices from 'block=' module param.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	 * Even if one block device in the param list fails to come up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	 * still allow the module to load and leave any others up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	ubiblock_create_from_param();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	 * Block devices are only created upon user requests, so we ignore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	 * existing volumes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	ret = ubi_register_volume_notifier(&ubiblock_notifier, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		goto err_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) err_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	unregister_blkdev(ubiblock_major, "ubiblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	ubiblock_remove_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) void __exit ubiblock_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	ubi_unregister_volume_notifier(&ubiblock_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	ubiblock_remove_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	unregister_blkdev(ubiblock_major, "ubiblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }