Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1 | /* |
| 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 Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 22 | * 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. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 26 | * |
| 27 | * Gluebi emulates MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 28 | * size (@mtd->writesize) is equivalent to the UBI minimal I/O unit. The |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 29 | * eraseblock size is equivalent to the logical eraseblock size of the volume. |
| 30 | */ |
| 31 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 32 | #include <linux/err.h> |
| 33 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame^] | 34 | #include <linux/slab.h> |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 35 | #include <linux/sched.h> |
Artem Bityutskiy | 3013ee3 | 2009-01-16 19:08:43 +0200 | [diff] [blame] | 36 | #include <linux/math64.h> |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 37 | #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, ...) \ |
| 44 | printk(KERN_DEBUG "gluebi (pid %d): %s: " fmt "\n", \ |
| 45 | 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 | */ |
| 56 | struct 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 */ |
| 66 | static LIST_HEAD(gluebi_devices); |
| 67 | static 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 | */ |
| 79 | static 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. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 88 | |
| 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 | */ |
| 97 | static int gluebi_get_device(struct mtd_info *mtd) |
| 98 | { |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 99 | struct gluebi_device *gluebi; |
| 100 | int ubi_mode = UBI_READONLY; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 101 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 102 | if (!try_module_get(THIS_MODULE)) |
| 103 | return -ENODEV; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 104 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 105 | if (mtd->flags & MTD_WRITEABLE) |
| 106 | ubi_mode = UBI_READWRITE; |
| 107 | |
| 108 | gluebi = container_of(mtd, struct gluebi_device, mtd); |
| 109 | mutex_lock(&devices_mutex); |
| 110 | if (gluebi->refcnt > 0) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 111 | /* |
| 112 | * The MTD device is already referenced and this is just one |
| 113 | * more reference. MTD allows many users to open the same |
| 114 | * volume simultaneously and do not distinguish between |
| 115 | * readers/writers/exclusive openers as UBI does. So we do not |
| 116 | * open the UBI volume again - just increase the reference |
| 117 | * counter and return. |
| 118 | */ |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 119 | gluebi->refcnt += 1; |
| 120 | mutex_unlock(&devices_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * This is the first reference to this UBI volume via the MTD device |
| 126 | * interface. Open the corresponding volume in read-write mode. |
| 127 | */ |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 128 | gluebi->desc = ubi_open_volume(gluebi->ubi_num, gluebi->vol_id, |
| 129 | ubi_mode); |
| 130 | if (IS_ERR(gluebi->desc)) { |
| 131 | mutex_unlock(&devices_mutex); |
| 132 | module_put(THIS_MODULE); |
| 133 | return PTR_ERR(gluebi->desc); |
| 134 | } |
| 135 | gluebi->refcnt += 1; |
| 136 | mutex_unlock(&devices_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * gluebi_put_device - put MTD device reference. |
| 142 | * @mtd: the MTD device description object |
| 143 | * |
| 144 | * This function is called every time the MTD device is being put. Returns |
| 145 | * zero in case of success and a negative error code in case of failure. |
| 146 | */ |
| 147 | static void gluebi_put_device(struct mtd_info *mtd) |
| 148 | { |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 149 | struct gluebi_device *gluebi; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 150 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 151 | gluebi = container_of(mtd, struct gluebi_device, mtd); |
| 152 | mutex_lock(&devices_mutex); |
| 153 | gluebi->refcnt -= 1; |
| 154 | if (gluebi->refcnt == 0) |
| 155 | ubi_close_volume(gluebi->desc); |
| 156 | module_put(THIS_MODULE); |
| 157 | mutex_unlock(&devices_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
| 161 | * gluebi_read - read operation of emulated MTD devices. |
| 162 | * @mtd: MTD device description object |
| 163 | * @from: absolute offset from where to read |
| 164 | * @len: how many bytes to read |
| 165 | * @retlen: count of read bytes is returned here |
| 166 | * @buf: buffer to store the read data |
| 167 | * |
| 168 | * This function returns zero in case of success and a negative error code in |
| 169 | * case of failure. |
| 170 | */ |
| 171 | static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 172 | size_t *retlen, unsigned char *buf) |
| 173 | { |
| 174 | int err = 0, lnum, offs, total_read; |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 175 | struct gluebi_device *gluebi; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 176 | |
| 177 | if (len < 0 || from < 0 || from + len > mtd->size) |
| 178 | return -EINVAL; |
| 179 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 180 | gluebi = container_of(mtd, struct gluebi_device, mtd); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 181 | |
Artem Bityutskiy | 3013ee3 | 2009-01-16 19:08:43 +0200 | [diff] [blame] | 182 | lnum = div_u64_rem(from, mtd->erasesize, &offs); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 183 | total_read = len; |
| 184 | while (total_read) { |
| 185 | size_t to_read = mtd->erasesize - offs; |
| 186 | |
| 187 | if (to_read > total_read) |
| 188 | to_read = total_read; |
| 189 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 190 | err = ubi_read(gluebi->desc, lnum, buf, offs, to_read); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 191 | if (err) |
| 192 | break; |
| 193 | |
| 194 | lnum += 1; |
| 195 | offs = 0; |
| 196 | total_read -= to_read; |
| 197 | buf += to_read; |
| 198 | } |
| 199 | |
| 200 | *retlen = len - total_read; |
| 201 | return err; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * gluebi_write - write operation of emulated MTD devices. |
| 206 | * @mtd: MTD device description object |
| 207 | * @to: absolute offset where to write |
| 208 | * @len: how many bytes to write |
| 209 | * @retlen: count of written bytes is returned here |
| 210 | * @buf: buffer with data to write |
| 211 | * |
| 212 | * This function returns zero in case of success and a negative error code in |
| 213 | * case of failure. |
| 214 | */ |
| 215 | static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len, |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 216 | size_t *retlen, const u_char *buf) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 217 | { |
| 218 | int err = 0, lnum, offs, total_written; |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 219 | struct gluebi_device *gluebi; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 220 | |
| 221 | if (len < 0 || to < 0 || len + to > mtd->size) |
| 222 | return -EINVAL; |
| 223 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 224 | gluebi = container_of(mtd, struct gluebi_device, mtd); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 225 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 226 | if (!(mtd->flags & MTD_WRITEABLE)) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 227 | return -EROFS; |
| 228 | |
Artem Bityutskiy | 3013ee3 | 2009-01-16 19:08:43 +0200 | [diff] [blame] | 229 | lnum = div_u64_rem(to, mtd->erasesize, &offs); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 230 | |
| 231 | if (len % mtd->writesize || offs % mtd->writesize) |
| 232 | return -EINVAL; |
| 233 | |
| 234 | total_written = len; |
| 235 | while (total_written) { |
| 236 | size_t to_write = mtd->erasesize - offs; |
| 237 | |
| 238 | if (to_write > total_written) |
| 239 | to_write = total_written; |
| 240 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 241 | err = ubi_write(gluebi->desc, lnum, buf, offs, to_write); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 242 | if (err) |
| 243 | break; |
| 244 | |
| 245 | lnum += 1; |
| 246 | offs = 0; |
| 247 | total_written -= to_write; |
| 248 | buf += to_write; |
| 249 | } |
| 250 | |
| 251 | *retlen = len - total_written; |
| 252 | return err; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * gluebi_erase - erase operation of emulated MTD devices. |
| 257 | * @mtd: the MTD device description object |
| 258 | * @instr: the erase operation description |
| 259 | * |
| 260 | * This function calls the erase callback when finishes. Returns zero in case |
| 261 | * of success and a negative error code in case of failure. |
| 262 | */ |
| 263 | static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 264 | { |
| 265 | int err, i, lnum, count; |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 266 | struct gluebi_device *gluebi; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 267 | |
| 268 | if (instr->addr < 0 || instr->addr > mtd->size - mtd->erasesize) |
| 269 | return -EINVAL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 270 | if (instr->len < 0 || instr->addr + instr->len > mtd->size) |
| 271 | return -EINVAL; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 272 | if (mtd_mod_by_ws(instr->addr, mtd) || mtd_mod_by_ws(instr->len, mtd)) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 273 | return -EINVAL; |
| 274 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 275 | lnum = mtd_div_by_eb(instr->addr, mtd); |
| 276 | count = mtd_div_by_eb(instr->len, mtd); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 277 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 278 | gluebi = container_of(mtd, struct gluebi_device, mtd); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 279 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 280 | if (!(mtd->flags & MTD_WRITEABLE)) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 281 | return -EROFS; |
| 282 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 283 | for (i = 0; i < count - 1; i++) { |
| 284 | err = ubi_leb_unmap(gluebi->desc, lnum + i); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 285 | if (err) |
| 286 | goto out_err; |
| 287 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 288 | /* |
| 289 | * MTD erase operations are synchronous, so we have to make sure the |
| 290 | * physical eraseblock is wiped out. |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 291 | * |
| 292 | * Thus, perform leb_erase instead of leb_unmap operation - leb_erase |
| 293 | * will wait for the end of operations |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 294 | */ |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 295 | err = ubi_leb_erase(gluebi->desc, lnum + i); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 296 | if (err) |
| 297 | goto out_err; |
| 298 | |
Artem Bityutskiy | 9c9ec14 | 2008-07-18 13:19:52 +0300 | [diff] [blame] | 299 | instr->state = MTD_ERASE_DONE; |
| 300 | mtd_erase_callback(instr); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 301 | return 0; |
| 302 | |
| 303 | out_err: |
| 304 | instr->state = MTD_ERASE_FAILED; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 305 | instr->fail_addr = (long long)lnum * mtd->erasesize; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 306 | return err; |
| 307 | } |
| 308 | |
| 309 | /** |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 310 | * gluebi_create - create a gluebi device for an UBI volume. |
| 311 | * @di: UBI device description object |
| 312 | * @vi: UBI volume description object |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 313 | * |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 314 | * This function is called when a new UBI volume is created in order to create |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 315 | * corresponding fake MTD device. Returns zero in case of success and a |
| 316 | * negative error code in case of failure. |
| 317 | */ |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 318 | static int gluebi_create(struct ubi_device_info *di, |
| 319 | struct ubi_volume_info *vi) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 320 | { |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 321 | struct gluebi_device *gluebi, *g; |
| 322 | struct mtd_info *mtd; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 323 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 324 | gluebi = kzalloc(sizeof(struct gluebi_device), GFP_KERNEL); |
| 325 | if (!gluebi) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 326 | return -ENOMEM; |
| 327 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 328 | mtd = &gluebi->mtd; |
| 329 | mtd->name = kmemdup(vi->name, vi->name_len + 1, GFP_KERNEL); |
| 330 | if (!mtd->name) { |
| 331 | kfree(gluebi); |
| 332 | return -ENOMEM; |
| 333 | } |
| 334 | |
| 335 | gluebi->vol_id = vi->vol_id; |
Artem Bityutskiy | c8cc452 | 2009-07-10 16:59:36 +0300 | [diff] [blame] | 336 | gluebi->ubi_num = vi->ubi_num; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 337 | mtd->type = MTD_UBIVOLUME; |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 338 | if (!di->ro_mode) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 339 | mtd->flags = MTD_WRITEABLE; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 340 | mtd->owner = THIS_MODULE; |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 341 | mtd->writesize = di->min_io_size; |
| 342 | mtd->erasesize = vi->usable_leb_size; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 343 | mtd->read = gluebi_read; |
| 344 | mtd->write = gluebi_write; |
| 345 | mtd->erase = gluebi_erase; |
| 346 | mtd->get_device = gluebi_get_device; |
| 347 | mtd->put_device = gluebi_put_device; |
| 348 | |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 349 | /* |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 350 | * In case of dynamic a volume, MTD device size is just volume size. In |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 351 | * case of a static volume the size is equivalent to the amount of data |
Jan Altenberg | cbd8a9d | 2008-03-28 16:13:53 +0100 | [diff] [blame] | 352 | * bytes. |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 353 | */ |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 354 | if (vi->vol_type == UBI_DYNAMIC_VOLUME) |
| 355 | mtd->size = (unsigned long long)vi->usable_leb_size * vi->size; |
Jan Altenberg | cbd8a9d | 2008-03-28 16:13:53 +0100 | [diff] [blame] | 356 | else |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 357 | mtd->size = vi->used_bytes; |
| 358 | |
| 359 | /* Just a sanity check - make sure this gluebi device does not exist */ |
| 360 | mutex_lock(&devices_mutex); |
| 361 | g = find_gluebi_nolock(vi->ubi_num, vi->vol_id); |
| 362 | if (g) |
| 363 | err_msg("gluebi MTD device %d form UBI device %d volume %d " |
| 364 | "already exists", g->mtd.index, vi->ubi_num, |
| 365 | vi->vol_id); |
| 366 | mutex_unlock(&devices_mutex); |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 367 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 368 | if (add_mtd_device(mtd)) { |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 369 | err_msg("cannot add MTD device"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 370 | kfree(mtd->name); |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 371 | kfree(gluebi); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 372 | return -ENFILE; |
| 373 | } |
| 374 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 375 | mutex_lock(&devices_mutex); |
| 376 | list_add_tail(&gluebi->list, &gluebi_devices); |
| 377 | mutex_unlock(&devices_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /** |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 382 | * gluebi_remove - remove a gluebi device. |
| 383 | * @vi: UBI volume description object |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 384 | * |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 385 | * This function is called when an UBI volume is removed and it removes |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 386 | * corresponding fake MTD device. Returns zero in case of success and a |
| 387 | * negative error code in case of failure. |
| 388 | */ |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 389 | static int gluebi_remove(struct ubi_volume_info *vi) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 390 | { |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 391 | int err = 0; |
| 392 | struct mtd_info *mtd; |
| 393 | struct gluebi_device *gluebi; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 394 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 395 | mutex_lock(&devices_mutex); |
| 396 | gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id); |
| 397 | if (!gluebi) { |
| 398 | err_msg("got remove notification for unknown UBI device %d " |
| 399 | "volume %d", vi->ubi_num, vi->vol_id); |
| 400 | err = -ENOENT; |
| 401 | } else if (gluebi->refcnt) |
| 402 | err = -EBUSY; |
| 403 | else |
| 404 | list_del(&gluebi->list); |
| 405 | mutex_unlock(&devices_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 406 | if (err) |
| 407 | return err; |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 408 | |
| 409 | mtd = &gluebi->mtd; |
| 410 | err = del_mtd_device(mtd); |
| 411 | if (err) { |
| 412 | err_msg("cannot remove fake MTD device %d, UBI device %d, " |
| 413 | "volume %d, error %d", mtd->index, gluebi->ubi_num, |
| 414 | gluebi->vol_id, err); |
| 415 | mutex_lock(&devices_mutex); |
| 416 | list_add_tail(&gluebi->list, &gluebi_devices); |
| 417 | mutex_unlock(&devices_mutex); |
| 418 | return err; |
| 419 | } |
| 420 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 421 | kfree(mtd->name); |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 422 | kfree(gluebi); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 423 | return 0; |
| 424 | } |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 425 | |
| 426 | /** |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 427 | * gluebi_updated - UBI volume was updated notifier. |
| 428 | * @vi: volume info structure |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 429 | * |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 430 | * This function is called every time an UBI volume is updated. It does nothing |
| 431 | * if te volume @vol is dynamic, and changes MTD device size if the |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 432 | * volume is static. This is needed because static volumes cannot be read past |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 433 | * data they contain. This function returns zero in case of success and a |
| 434 | * negative error code in case of error. |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 435 | */ |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 436 | static int gluebi_updated(struct ubi_volume_info *vi) |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 437 | { |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 438 | struct gluebi_device *gluebi; |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 439 | |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 440 | mutex_lock(&devices_mutex); |
| 441 | gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id); |
| 442 | if (!gluebi) { |
| 443 | mutex_unlock(&devices_mutex); |
| 444 | err_msg("got update notification for unknown UBI device %d " |
| 445 | "volume %d", vi->ubi_num, vi->vol_id); |
| 446 | return -ENOENT; |
| 447 | } |
| 448 | |
| 449 | if (vi->vol_type == UBI_STATIC_VOLUME) |
| 450 | gluebi->mtd.size = vi->used_bytes; |
| 451 | mutex_unlock(&devices_mutex); |
| 452 | return 0; |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 453 | } |
Dmitry Pervushin | 2ba3d76 | 2009-05-31 18:32:59 +0400 | [diff] [blame] | 454 | |
| 455 | /** |
| 456 | * gluebi_resized - UBI volume was re-sized notifier. |
| 457 | * @vi: volume info structure |
| 458 | * |
| 459 | * This function is called every time an UBI volume is re-size. It changes the |
| 460 | * corresponding fake MTD device size. This function returns zero in case of |
| 461 | * success and a negative error code in case of error. |
| 462 | */ |
| 463 | static int gluebi_resized(struct ubi_volume_info *vi) |
| 464 | { |
| 465 | struct gluebi_device *gluebi; |
| 466 | |
| 467 | mutex_lock(&devices_mutex); |
| 468 | gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id); |
| 469 | if (!gluebi) { |
| 470 | mutex_unlock(&devices_mutex); |
| 471 | err_msg("got update notification for unknown UBI device %d " |
| 472 | "volume %d", vi->ubi_num, vi->vol_id); |
| 473 | return -ENOENT; |
| 474 | } |
| 475 | gluebi->mtd.size = vi->used_bytes; |
| 476 | mutex_unlock(&devices_mutex); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * gluebi_notify - UBI notification handler. |
| 482 | * @nb: registered notifier block |
| 483 | * @l: notification type |
| 484 | * @ptr: pointer to the &struct ubi_notification object |
| 485 | */ |
| 486 | static int gluebi_notify(struct notifier_block *nb, unsigned long l, |
| 487 | void *ns_ptr) |
| 488 | { |
| 489 | struct ubi_notification *nt = ns_ptr; |
| 490 | |
| 491 | switch (l) { |
| 492 | case UBI_VOLUME_ADDED: |
| 493 | gluebi_create(&nt->di, &nt->vi); |
| 494 | break; |
| 495 | case UBI_VOLUME_REMOVED: |
| 496 | gluebi_remove(&nt->vi); |
| 497 | break; |
| 498 | case UBI_VOLUME_RESIZED: |
| 499 | gluebi_resized(&nt->vi); |
| 500 | break; |
| 501 | case UBI_VOLUME_UPDATED: |
| 502 | gluebi_updated(&nt->vi); |
| 503 | break; |
| 504 | default: |
| 505 | break; |
| 506 | } |
| 507 | return NOTIFY_OK; |
| 508 | } |
| 509 | |
| 510 | static struct notifier_block gluebi_notifier = { |
| 511 | .notifier_call = gluebi_notify, |
| 512 | }; |
| 513 | |
| 514 | static int __init ubi_gluebi_init(void) |
| 515 | { |
| 516 | return ubi_register_volume_notifier(&gluebi_notifier, 0); |
| 517 | } |
| 518 | |
| 519 | static void __exit ubi_gluebi_exit(void) |
| 520 | { |
| 521 | struct gluebi_device *gluebi, *g; |
| 522 | |
| 523 | list_for_each_entry_safe(gluebi, g, &gluebi_devices, list) { |
| 524 | int err; |
| 525 | struct mtd_info *mtd = &gluebi->mtd; |
| 526 | |
| 527 | err = del_mtd_device(mtd); |
| 528 | if (err) |
| 529 | err_msg("error %d while removing gluebi MTD device %d, " |
| 530 | "UBI device %d, volume %d - ignoring", err, |
| 531 | mtd->index, gluebi->ubi_num, gluebi->vol_id); |
| 532 | kfree(mtd->name); |
| 533 | kfree(gluebi); |
| 534 | } |
| 535 | ubi_unregister_volume_notifier(&gluebi_notifier); |
| 536 | } |
| 537 | |
| 538 | module_init(ubi_gluebi_init); |
| 539 | module_exit(ubi_gluebi_exit); |
| 540 | MODULE_DESCRIPTION("MTD emulation layer over UBI volumes"); |
| 541 | MODULE_AUTHOR("Artem Bityutskiy, Joern Engel"); |
| 542 | MODULE_LICENSE("GPL"); |