blob: 1cb287ec32adbb486c3289c23a89191e7be48221 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Artem Bityutskiy (Битюцкий Артём), Joern Engel
19 */
20
21/*
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +040022 * This is a small driver which implements fake MTD devices on top of UBI
23 * volumes. This sounds strange, but it is in fact quite useful to make
24 * MTD-oriented software (including all the legacy software) work on top of
25 * UBI.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040026 *
27 * Gluebi emulates MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +040028 * size (@mtd->writesize) is equivalent to the UBI minimal I/O unit. The
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040029 * eraseblock size is equivalent to the logical eraseblock size of the volume.
30 */
31
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +040032#include <linux/err.h>
33#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +040035#include <linux/sched.h>
Artem Bityutskiy3013ee32009-01-16 19:08:43 +020036#include <linux/math64.h>
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +040037#include <linux/module.h>
38#include <linux/mutex.h>
39#include <linux/mtd/ubi.h>
40#include <linux/mtd/mtd.h>
41#include "ubi-media.h"
42
43#define err_msg(fmt, ...) \
Artem Bityutskiye28453b2012-08-27 15:13:05 +030044 pr_err("gluebi (pid %d): %s: " fmt "\n", \
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +040045 current->pid, __func__, ##__VA_ARGS__)
46
47/**
48 * struct gluebi_device - a gluebi device description data structure.
49 * @mtd: emulated MTD device description object
50 * @refcnt: gluebi device reference count
51 * @desc: UBI volume descriptor
52 * @ubi_num: UBI device number this gluebi device works on
53 * @vol_id: ID of UBI volume this gluebi device works on
54 * @list: link in a list of gluebi devices
55 */
56struct gluebi_device {
57 struct mtd_info mtd;
58 int refcnt;
59 struct ubi_volume_desc *desc;
60 int ubi_num;
61 int vol_id;
62 struct list_head list;
63};
64
65/* List of all gluebi devices */
66static LIST_HEAD(gluebi_devices);
67static DEFINE_MUTEX(devices_mutex);
68
69/**
70 * find_gluebi_nolock - find a gluebi device.
71 * @ubi_num: UBI device number
72 * @vol_id: volume ID
73 *
74 * This function seraches for gluebi device corresponding to UBI device
75 * @ubi_num and UBI volume @vol_id. Returns the gluebi device description
76 * object in case of success and %NULL in case of failure. The caller has to
77 * have the &devices_mutex locked.
78 */
79static struct gluebi_device *find_gluebi_nolock(int ubi_num, int vol_id)
80{
81 struct gluebi_device *gluebi;
82
83 list_for_each_entry(gluebi, &gluebi_devices, list)
84 if (gluebi->ubi_num == ubi_num && gluebi->vol_id == vol_id)
85 return gluebi;
86 return NULL;
87}
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040088
89/**
90 * gluebi_get_device - get MTD device reference.
91 * @mtd: the MTD device description object
92 *
93 * This function is called every time the MTD device is being opened and
94 * implements the MTD get_device() operation. Returns zero in case of success
95 * and a negative error code in case of failure.
96 */
97static int gluebi_get_device(struct mtd_info *mtd)
98{
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +040099 struct gluebi_device *gluebi;
100 int ubi_mode = UBI_READONLY;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400101
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400102 if (mtd->flags & MTD_WRITEABLE)
103 ubi_mode = UBI_READWRITE;
104
105 gluebi = container_of(mtd, struct gluebi_device, mtd);
106 mutex_lock(&devices_mutex);
107 if (gluebi->refcnt > 0) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400108 /*
109 * The MTD device is already referenced and this is just one
110 * more reference. MTD allows many users to open the same
111 * volume simultaneously and do not distinguish between
Andrew Murray061eebb2015-08-10 12:02:09 +0100112 * readers/writers/exclusive/meta openers as UBI does. So we do
113 * not open the UBI volume again - just increase the reference
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400114 * counter and return.
115 */
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400116 gluebi->refcnt += 1;
117 mutex_unlock(&devices_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400118 return 0;
119 }
120
121 /*
122 * This is the first reference to this UBI volume via the MTD device
123 * interface. Open the corresponding volume in read-write mode.
124 */
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400125 gluebi->desc = ubi_open_volume(gluebi->ubi_num, gluebi->vol_id,
126 ubi_mode);
127 if (IS_ERR(gluebi->desc)) {
128 mutex_unlock(&devices_mutex);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400129 return PTR_ERR(gluebi->desc);
130 }
131 gluebi->refcnt += 1;
132 mutex_unlock(&devices_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400133 return 0;
134}
135
136/**
137 * gluebi_put_device - put MTD device reference.
138 * @mtd: the MTD device description object
139 *
140 * This function is called every time the MTD device is being put. Returns
141 * zero in case of success and a negative error code in case of failure.
142 */
143static void gluebi_put_device(struct mtd_info *mtd)
144{
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400145 struct gluebi_device *gluebi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400146
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400147 gluebi = container_of(mtd, struct gluebi_device, mtd);
148 mutex_lock(&devices_mutex);
149 gluebi->refcnt -= 1;
150 if (gluebi->refcnt == 0)
151 ubi_close_volume(gluebi->desc);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400152 mutex_unlock(&devices_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400153}
154
155/**
156 * gluebi_read - read operation of emulated MTD devices.
157 * @mtd: MTD device description object
158 * @from: absolute offset from where to read
159 * @len: how many bytes to read
160 * @retlen: count of read bytes is returned here
161 * @buf: buffer to store the read data
162 *
163 * This function returns zero in case of success and a negative error code in
164 * case of failure.
165 */
166static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
167 size_t *retlen, unsigned char *buf)
168{
Ezequiel Garcia41c04382012-11-19 20:35:20 -0300169 int err = 0, lnum, offs, bytes_left;
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400170 struct gluebi_device *gluebi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400171
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400172 gluebi = container_of(mtd, struct gluebi_device, mtd);
Artem Bityutskiy3013ee32009-01-16 19:08:43 +0200173 lnum = div_u64_rem(from, mtd->erasesize, &offs);
Ezequiel Garcia41c04382012-11-19 20:35:20 -0300174 bytes_left = len;
175 while (bytes_left) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400176 size_t to_read = mtd->erasesize - offs;
177
Ezequiel Garcia41c04382012-11-19 20:35:20 -0300178 if (to_read > bytes_left)
179 to_read = bytes_left;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400180
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400181 err = ubi_read(gluebi->desc, lnum, buf, offs, to_read);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400182 if (err)
183 break;
184
185 lnum += 1;
186 offs = 0;
Ezequiel Garcia41c04382012-11-19 20:35:20 -0300187 bytes_left -= to_read;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400188 buf += to_read;
189 }
190
Ezequiel Garcia41c04382012-11-19 20:35:20 -0300191 *retlen = len - bytes_left;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400192 return err;
193}
194
195/**
196 * gluebi_write - write operation of emulated MTD devices.
197 * @mtd: MTD device description object
198 * @to: absolute offset where to write
199 * @len: how many bytes to write
200 * @retlen: count of written bytes is returned here
201 * @buf: buffer with data to write
202 *
203 * This function returns zero in case of success and a negative error code in
204 * case of failure.
205 */
206static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400207 size_t *retlen, const u_char *buf)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400208{
Ezequiel Garcia41c04382012-11-19 20:35:20 -0300209 int err = 0, lnum, offs, bytes_left;
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400210 struct gluebi_device *gluebi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400211
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400212 gluebi = container_of(mtd, struct gluebi_device, mtd);
Artem Bityutskiy3013ee32009-01-16 19:08:43 +0200213 lnum = div_u64_rem(to, mtd->erasesize, &offs);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400214
215 if (len % mtd->writesize || offs % mtd->writesize)
216 return -EINVAL;
217
Ezequiel Garcia41c04382012-11-19 20:35:20 -0300218 bytes_left = len;
219 while (bytes_left) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400220 size_t to_write = mtd->erasesize - offs;
221
Ezequiel Garcia41c04382012-11-19 20:35:20 -0300222 if (to_write > bytes_left)
223 to_write = bytes_left;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400224
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200225 err = ubi_leb_write(gluebi->desc, lnum, buf, offs, to_write);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400226 if (err)
227 break;
228
229 lnum += 1;
230 offs = 0;
Ezequiel Garcia41c04382012-11-19 20:35:20 -0300231 bytes_left -= to_write;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400232 buf += to_write;
233 }
234
Ezequiel Garcia41c04382012-11-19 20:35:20 -0300235 *retlen = len - bytes_left;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400236 return err;
237}
238
239/**
240 * gluebi_erase - erase operation of emulated MTD devices.
241 * @mtd: the MTD device description object
242 * @instr: the erase operation description
243 *
244 * This function calls the erase callback when finishes. Returns zero in case
245 * of success and a negative error code in case of failure.
246 */
247static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
248{
249 int err, i, lnum, count;
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400250 struct gluebi_device *gluebi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400251
Adrian Hunter69423d92008-12-10 13:37:21 +0000252 if (mtd_mod_by_ws(instr->addr, mtd) || mtd_mod_by_ws(instr->len, mtd))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400253 return -EINVAL;
254
Adrian Hunter69423d92008-12-10 13:37:21 +0000255 lnum = mtd_div_by_eb(instr->addr, mtd);
256 count = mtd_div_by_eb(instr->len, mtd);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400257 gluebi = container_of(mtd, struct gluebi_device, mtd);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400258
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400259 for (i = 0; i < count - 1; i++) {
260 err = ubi_leb_unmap(gluebi->desc, lnum + i);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400261 if (err)
262 goto out_err;
263 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400264 /*
265 * MTD erase operations are synchronous, so we have to make sure the
266 * physical eraseblock is wiped out.
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400267 *
268 * Thus, perform leb_erase instead of leb_unmap operation - leb_erase
269 * will wait for the end of operations
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400270 */
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400271 err = ubi_leb_erase(gluebi->desc, lnum + i);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400272 if (err)
273 goto out_err;
274
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300275 instr->state = MTD_ERASE_DONE;
276 mtd_erase_callback(instr);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400277 return 0;
278
279out_err:
280 instr->state = MTD_ERASE_FAILED;
Adrian Hunter69423d92008-12-10 13:37:21 +0000281 instr->fail_addr = (long long)lnum * mtd->erasesize;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400282 return err;
283}
284
285/**
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400286 * gluebi_create - create a gluebi device for an UBI volume.
287 * @di: UBI device description object
288 * @vi: UBI volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400289 *
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400290 * This function is called when a new UBI volume is created in order to create
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400291 * corresponding fake MTD device. Returns zero in case of success and a
292 * negative error code in case of failure.
293 */
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400294static int gluebi_create(struct ubi_device_info *di,
295 struct ubi_volume_info *vi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400296{
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400297 struct gluebi_device *gluebi, *g;
298 struct mtd_info *mtd;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400299
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400300 gluebi = kzalloc(sizeof(struct gluebi_device), GFP_KERNEL);
301 if (!gluebi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400302 return -ENOMEM;
303
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400304 mtd = &gluebi->mtd;
305 mtd->name = kmemdup(vi->name, vi->name_len + 1, GFP_KERNEL);
306 if (!mtd->name) {
307 kfree(gluebi);
308 return -ENOMEM;
309 }
310
311 gluebi->vol_id = vi->vol_id;
Artem Bityutskiyc8cc4522009-07-10 16:59:36 +0300312 gluebi->ubi_num = vi->ubi_num;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400313 mtd->type = MTD_UBIVOLUME;
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400314 if (!di->ro_mode)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400315 mtd->flags = MTD_WRITEABLE;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400316 mtd->owner = THIS_MODULE;
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400317 mtd->writesize = di->min_io_size;
318 mtd->erasesize = vi->usable_leb_size;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200319 mtd->_read = gluebi_read;
320 mtd->_write = gluebi_write;
321 mtd->_erase = gluebi_erase;
322 mtd->_get_device = gluebi_get_device;
323 mtd->_put_device = gluebi_put_device;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400324
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300325 /*
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400326 * In case of dynamic a volume, MTD device size is just volume size. In
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300327 * case of a static volume the size is equivalent to the amount of data
Jan Altenbergcbd8a9d2008-03-28 16:13:53 +0100328 * bytes.
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300329 */
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400330 if (vi->vol_type == UBI_DYNAMIC_VOLUME)
331 mtd->size = (unsigned long long)vi->usable_leb_size * vi->size;
Jan Altenbergcbd8a9d2008-03-28 16:13:53 +0100332 else
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400333 mtd->size = vi->used_bytes;
334
335 /* Just a sanity check - make sure this gluebi device does not exist */
336 mutex_lock(&devices_mutex);
337 g = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
338 if (g)
Artem Bityutskiy049333c2012-08-27 14:43:54 +0300339 err_msg("gluebi MTD device %d form UBI device %d volume %d already exists",
340 g->mtd.index, vi->ubi_num, vi->vol_id);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400341 mutex_unlock(&devices_mutex);
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300342
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100343 if (mtd_device_register(mtd, NULL, 0)) {
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400344 err_msg("cannot add MTD device");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400345 kfree(mtd->name);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400346 kfree(gluebi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400347 return -ENFILE;
348 }
349
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400350 mutex_lock(&devices_mutex);
351 list_add_tail(&gluebi->list, &gluebi_devices);
352 mutex_unlock(&devices_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400353 return 0;
354}
355
356/**
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400357 * gluebi_remove - remove a gluebi device.
358 * @vi: UBI volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400359 *
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400360 * This function is called when an UBI volume is removed and it removes
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400361 * corresponding fake MTD device. Returns zero in case of success and a
362 * negative error code in case of failure.
363 */
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400364static int gluebi_remove(struct ubi_volume_info *vi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400365{
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400366 int err = 0;
367 struct mtd_info *mtd;
368 struct gluebi_device *gluebi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400369
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400370 mutex_lock(&devices_mutex);
371 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
372 if (!gluebi) {
Artem Bityutskiy049333c2012-08-27 14:43:54 +0300373 err_msg("got remove notification for unknown UBI device %d volume %d",
374 vi->ubi_num, vi->vol_id);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400375 err = -ENOENT;
376 } else if (gluebi->refcnt)
377 err = -EBUSY;
378 else
379 list_del(&gluebi->list);
380 mutex_unlock(&devices_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400381 if (err)
382 return err;
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400383
384 mtd = &gluebi->mtd;
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100385 err = mtd_device_unregister(mtd);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400386 if (err) {
Artem Bityutskiy049333c2012-08-27 14:43:54 +0300387 err_msg("cannot remove fake MTD device %d, UBI device %d, volume %d, error %d",
388 mtd->index, gluebi->ubi_num, gluebi->vol_id, err);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400389 mutex_lock(&devices_mutex);
390 list_add_tail(&gluebi->list, &gluebi_devices);
391 mutex_unlock(&devices_mutex);
392 return err;
393 }
394
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400395 kfree(mtd->name);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400396 kfree(gluebi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400397 return 0;
398}
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300399
400/**
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400401 * gluebi_updated - UBI volume was updated notifier.
402 * @vi: volume info structure
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300403 *
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400404 * This function is called every time an UBI volume is updated. It does nothing
405 * if te volume @vol is dynamic, and changes MTD device size if the
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300406 * volume is static. This is needed because static volumes cannot be read past
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400407 * data they contain. This function returns zero in case of success and a
408 * negative error code in case of error.
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300409 */
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400410static int gluebi_updated(struct ubi_volume_info *vi)
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300411{
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400412 struct gluebi_device *gluebi;
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300413
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400414 mutex_lock(&devices_mutex);
415 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
416 if (!gluebi) {
417 mutex_unlock(&devices_mutex);
Artem Bityutskiy049333c2012-08-27 14:43:54 +0300418 err_msg("got update notification for unknown UBI device %d volume %d",
419 vi->ubi_num, vi->vol_id);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400420 return -ENOENT;
421 }
422
423 if (vi->vol_type == UBI_STATIC_VOLUME)
424 gluebi->mtd.size = vi->used_bytes;
425 mutex_unlock(&devices_mutex);
426 return 0;
Artem Bityutskiy941dfb02007-05-05 16:33:13 +0300427}
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400428
429/**
430 * gluebi_resized - UBI volume was re-sized notifier.
431 * @vi: volume info structure
432 *
433 * This function is called every time an UBI volume is re-size. It changes the
434 * corresponding fake MTD device size. This function returns zero in case of
435 * success and a negative error code in case of error.
436 */
437static int gluebi_resized(struct ubi_volume_info *vi)
438{
439 struct gluebi_device *gluebi;
440
441 mutex_lock(&devices_mutex);
442 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
443 if (!gluebi) {
444 mutex_unlock(&devices_mutex);
Artem Bityutskiy049333c2012-08-27 14:43:54 +0300445 err_msg("got update notification for unknown UBI device %d volume %d",
446 vi->ubi_num, vi->vol_id);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400447 return -ENOENT;
448 }
449 gluebi->mtd.size = vi->used_bytes;
450 mutex_unlock(&devices_mutex);
451 return 0;
452}
453
454/**
455 * gluebi_notify - UBI notification handler.
456 * @nb: registered notifier block
457 * @l: notification type
458 * @ptr: pointer to the &struct ubi_notification object
459 */
460static int gluebi_notify(struct notifier_block *nb, unsigned long l,
461 void *ns_ptr)
462{
463 struct ubi_notification *nt = ns_ptr;
464
465 switch (l) {
466 case UBI_VOLUME_ADDED:
467 gluebi_create(&nt->di, &nt->vi);
468 break;
469 case UBI_VOLUME_REMOVED:
470 gluebi_remove(&nt->vi);
471 break;
472 case UBI_VOLUME_RESIZED:
473 gluebi_resized(&nt->vi);
474 break;
475 case UBI_VOLUME_UPDATED:
476 gluebi_updated(&nt->vi);
477 break;
478 default:
479 break;
480 }
481 return NOTIFY_OK;
482}
483
484static struct notifier_block gluebi_notifier = {
485 .notifier_call = gluebi_notify,
486};
487
488static int __init ubi_gluebi_init(void)
489{
490 return ubi_register_volume_notifier(&gluebi_notifier, 0);
491}
492
493static void __exit ubi_gluebi_exit(void)
494{
495 struct gluebi_device *gluebi, *g;
496
497 list_for_each_entry_safe(gluebi, g, &gluebi_devices, list) {
498 int err;
499 struct mtd_info *mtd = &gluebi->mtd;
500
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100501 err = mtd_device_unregister(mtd);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400502 if (err)
Artem Bityutskiy049333c2012-08-27 14:43:54 +0300503 err_msg("error %d while removing gluebi MTD device %d, UBI device %d, volume %d - ignoring",
504 err, mtd->index, gluebi->ubi_num,
505 gluebi->vol_id);
Dmitry Pervushin2ba3d762009-05-31 18:32:59 +0400506 kfree(mtd->name);
507 kfree(gluebi);
508 }
509 ubi_unregister_volume_notifier(&gluebi_notifier);
510}
511
512module_init(ubi_gluebi_init);
513module_exit(ubi_gluebi_exit);
514MODULE_DESCRIPTION("MTD emulation layer over UBI volumes");
515MODULE_AUTHOR("Artem Bityutskiy, Joern Engel");
516MODULE_LICENSE("GPL");