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