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)  * Copyright (c) International Business Machines Corp., 2006
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Author: Artem Bityutskiy (Битюцкий Артём), Joern Engel
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * This is a small driver which implements fake MTD devices on top of UBI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * volumes. This sounds strange, but it is in fact quite useful to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * MTD-oriented software (including all the legacy software) work on top of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * UBI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Gluebi emulates MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * size (@mtd->writesize) is equivalent to the UBI minimal I/O unit. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * eraseblock size is equivalent to the logical eraseblock size of the volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/math64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/mtd/ubi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include "ubi-media.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define err_msg(fmt, ...)                                   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	pr_err("gluebi (pid %d): %s: " fmt "\n",            \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	       current->pid, __func__, ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * struct gluebi_device - a gluebi device description data structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * @mtd: emulated MTD device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * @refcnt: gluebi device reference count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * @desc: UBI volume descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * @ubi_num: UBI device number this gluebi device works on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * @vol_id: ID of UBI volume this gluebi device works on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * @list: link in a list of gluebi devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) struct gluebi_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct mtd_info mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	int refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct ubi_volume_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	int ubi_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) /* List of all gluebi devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static LIST_HEAD(gluebi_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static DEFINE_MUTEX(devices_mutex);
^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)  * find_gluebi_nolock - find a gluebi device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * @ubi_num: UBI device number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * @vol_id: volume ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * This function seraches for gluebi device corresponding to UBI device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * @ubi_num and UBI volume @vol_id. Returns the gluebi device description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * object in case of success and %NULL in case of failure. The caller has to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * have the &devices_mutex locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static struct gluebi_device *find_gluebi_nolock(int ubi_num, int vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct gluebi_device *gluebi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	list_for_each_entry(gluebi, &gluebi_devices, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		if (gluebi->ubi_num == ubi_num && gluebi->vol_id == vol_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			return gluebi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * gluebi_get_device - get MTD device reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * @mtd: the MTD device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  * This function is called every time the MTD device is being opened and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * implements the MTD get_device() operation. Returns zero in case of success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static int gluebi_get_device(struct mtd_info *mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	struct gluebi_device *gluebi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	int ubi_mode = UBI_READONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (mtd->flags & MTD_WRITEABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		ubi_mode = UBI_READWRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	gluebi = container_of(mtd, struct gluebi_device, mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (gluebi->refcnt > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		 * The MTD device is already referenced and this is just one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		 * more reference. MTD allows many users to open the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		 * volume simultaneously and do not distinguish between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		 * readers/writers/exclusive/meta openers as UBI does. So we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		 * not open the UBI volume again - just increase the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		 * counter and return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		gluebi->refcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	 * This is the first reference to this UBI volume via the MTD device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	 * interface. Open the corresponding volume in read-write mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	gluebi->desc = ubi_open_volume(gluebi->ubi_num, gluebi->vol_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 				       ubi_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (IS_ERR(gluebi->desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return PTR_ERR(gluebi->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	gluebi->refcnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^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)  * gluebi_put_device - put MTD device reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  * @mtd: the MTD device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  * This function is called every time the MTD device is being put. Returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  * zero in case of success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void gluebi_put_device(struct mtd_info *mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	struct gluebi_device *gluebi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	gluebi = container_of(mtd, struct gluebi_device, mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	gluebi->refcnt -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (gluebi->refcnt == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		ubi_close_volume(gluebi->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  * gluebi_read - read operation of emulated MTD devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * @mtd: MTD device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  * @from: absolute offset from where to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  * @len: how many bytes to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  * @retlen: count of read bytes is returned here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  * @buf: buffer to store the read data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * This function returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		       size_t *retlen, unsigned char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	int err = 0, lnum, offs, bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	struct gluebi_device *gluebi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	gluebi = container_of(mtd, struct gluebi_device, mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	lnum = div_u64_rem(from, mtd->erasesize, &offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	bytes_left = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	while (bytes_left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		size_t to_read = mtd->erasesize - offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		if (to_read > bytes_left)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			to_read = bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		err = ubi_read(gluebi->desc, lnum, buf, offs, to_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		lnum += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		bytes_left -= to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		buf += to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	*retlen = len - bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * gluebi_write - write operation of emulated MTD devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * @mtd: MTD device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * @to: absolute offset where to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  * @len: how many bytes to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * @retlen: count of written bytes is returned here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  * @buf: buffer with data to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  * This function returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			size_t *retlen, const u_char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	int err = 0, lnum, offs, bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	struct gluebi_device *gluebi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	gluebi = container_of(mtd, struct gluebi_device, mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	lnum = div_u64_rem(to, mtd->erasesize, &offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (len % mtd->writesize || offs % mtd->writesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	bytes_left = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	while (bytes_left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		size_t to_write = mtd->erasesize - offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		if (to_write > bytes_left)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			to_write = bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		err = ubi_leb_write(gluebi->desc, lnum, buf, offs, to_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		lnum += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		bytes_left -= to_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		buf += to_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	*retlen = len - bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  * gluebi_erase - erase operation of emulated MTD devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  * @mtd: the MTD device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  * @instr: the erase operation description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  * This function calls the erase callback when finishes. Returns zero in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  * of success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	int err, i, lnum, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct gluebi_device *gluebi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (mtd_mod_by_ws(instr->addr, mtd) || mtd_mod_by_ws(instr->len, mtd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	lnum = mtd_div_by_eb(instr->addr, mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	count = mtd_div_by_eb(instr->len, mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	gluebi = container_of(mtd, struct gluebi_device, mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	for (i = 0; i < count - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		err = ubi_leb_unmap(gluebi->desc, lnum + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			goto out_err;
^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) 	 * MTD erase operations are synchronous, so we have to make sure the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	 * physical eraseblock is wiped out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	 * Thus, perform leb_erase instead of leb_unmap operation - leb_erase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	 * will wait for the end of operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	err = ubi_leb_erase(gluebi->desc, lnum + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	instr->fail_addr = (long long)lnum * mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)  * gluebi_create - create a gluebi device for an UBI volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)  * @di: UBI device description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)  * @vi: UBI volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)  * This function is called when a new UBI volume is created in order to create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)  * corresponding fake MTD device. Returns zero in case of success and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  * negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static int gluebi_create(struct ubi_device_info *di,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			 struct ubi_volume_info *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	struct gluebi_device *gluebi, *g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct mtd_info *mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	gluebi = kzalloc(sizeof(struct gluebi_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (!gluebi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	mtd = &gluebi->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	mtd->name = kmemdup(vi->name, vi->name_len + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (!mtd->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		kfree(gluebi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	gluebi->vol_id = vi->vol_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	gluebi->ubi_num = vi->ubi_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	mtd->type = MTD_UBIVOLUME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (!di->ro_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		mtd->flags = MTD_WRITEABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	mtd->owner      = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	mtd->writesize  = di->min_io_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	mtd->erasesize  = vi->usable_leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	mtd->_read       = gluebi_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	mtd->_write      = gluebi_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	mtd->_erase      = gluebi_erase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	mtd->_get_device = gluebi_get_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	mtd->_put_device = gluebi_put_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	 * In case of dynamic a volume, MTD device size is just volume size. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	 * case of a static volume the size is equivalent to the amount of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	 * bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (vi->vol_type == UBI_DYNAMIC_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		mtd->size = (unsigned long long)vi->usable_leb_size * vi->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		mtd->size = vi->used_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	/* Just a sanity check - make sure this gluebi device does not exist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	g = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (g)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		err_msg("gluebi MTD device %d form UBI device %d volume %d already exists",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			g->mtd.index, vi->ubi_num, vi->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (mtd_device_register(mtd, NULL, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		err_msg("cannot add MTD device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		kfree(mtd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		kfree(gluebi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return -ENFILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	list_add_tail(&gluebi->list, &gluebi_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	return 0;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  * gluebi_remove - remove a gluebi device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  * @vi: UBI volume description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * This function is called when an UBI volume is removed and it removes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  * corresponding fake MTD device. Returns zero in case of success and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  * negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static int gluebi_remove(struct ubi_volume_info *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	struct mtd_info *mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	struct gluebi_device *gluebi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (!gluebi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		err_msg("got remove notification for unknown UBI device %d volume %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			vi->ubi_num, vi->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	} else if (gluebi->refcnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		list_del(&gluebi->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	mtd = &gluebi->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	err = mtd_device_unregister(mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		err_msg("cannot remove fake MTD device %d, UBI device %d, volume %d, error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			mtd->index, gluebi->ubi_num, gluebi->vol_id, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		list_add_tail(&gluebi->list, &gluebi_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	kfree(mtd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	kfree(gluebi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^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)  * gluebi_updated - UBI volume was updated notifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)  * @vi: volume info structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)  * This function is called every time an UBI volume is updated. It does nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)  * if te volume @vol is dynamic, and changes MTD device size if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)  * volume is static. This is needed because static volumes cannot be read past
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)  * data they contain. This function returns zero in case of success and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)  * negative error code in case of error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static int gluebi_updated(struct ubi_volume_info *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	struct gluebi_device *gluebi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	if (!gluebi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		err_msg("got update notification for unknown UBI device %d volume %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			vi->ubi_num, vi->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	if (vi->vol_type == UBI_STATIC_VOLUME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		gluebi->mtd.size = vi->used_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)  * gluebi_resized - UBI volume was re-sized notifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)  * @vi: volume info structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)  * This function is called every time an UBI volume is re-size. It changes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)  * corresponding fake MTD device size. This function returns zero in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  * success and a negative error code in case of error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static int gluebi_resized(struct ubi_volume_info *vi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	struct gluebi_device *gluebi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	mutex_lock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	if (!gluebi) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		err_msg("got update notification for unknown UBI device %d volume %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			vi->ubi_num, vi->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	gluebi->mtd.size = vi->used_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	mutex_unlock(&devices_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)  * gluebi_notify - UBI notification handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)  * @nb: registered notifier block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)  * @l: notification type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)  * @ptr: pointer to the &struct ubi_notification object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static int gluebi_notify(struct notifier_block *nb, unsigned long l,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			 void *ns_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	struct ubi_notification *nt = ns_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	switch (l) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	case UBI_VOLUME_ADDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		gluebi_create(&nt->di, &nt->vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	case UBI_VOLUME_REMOVED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		gluebi_remove(&nt->vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	case UBI_VOLUME_RESIZED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		gluebi_resized(&nt->vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	case UBI_VOLUME_UPDATED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		gluebi_updated(&nt->vi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) static struct notifier_block gluebi_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	.notifier_call	= gluebi_notify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) static int __init ubi_gluebi_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	return ubi_register_volume_notifier(&gluebi_notifier, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static void __exit ubi_gluebi_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	struct gluebi_device *gluebi, *g;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	list_for_each_entry_safe(gluebi, g, &gluebi_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		struct mtd_info *mtd = &gluebi->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		err = mtd_device_unregister(mtd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			err_msg("error %d while removing gluebi MTD device %d, UBI device %d, volume %d - ignoring",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 				err, mtd->index, gluebi->ubi_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 				gluebi->vol_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		kfree(mtd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		kfree(gluebi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	ubi_unregister_volume_notifier(&gluebi_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) module_init(ubi_gluebi_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) module_exit(ubi_gluebi_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) MODULE_DESCRIPTION("MTD emulation layer over UBI volumes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) MODULE_AUTHOR("Artem Bityutskiy, Joern Engel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) MODULE_LICENSE("GPL");